I'm trying to retrieve record from the public database of CloudKit. (Swift version 5.6 , Xcode 13.3 and iOS 15.4)
From Content.view, I call the function vm.retrieve_T100 of ViewModel.swift. The argument of it is mail address user will input. I've set the print for debug.
I want to progress like *1,*2,*3,*3,*3,*4,*5. But Actually It progressed like *1,*2,*5,3,,3,*3,*4. How should I change the program?
probably I should stop the way of asynchronous processing, and I should re-write the program in synchronous processing. I don't know how to it.
ContentView.swift
struct ContentView: View {
@EnvironmentObject var vm: ViewModel
.....
var body: some View {
.....
func mail_choufuku_error_check(_:String) -> Bool {
**print("*1 before retrieve_T100 *")**
vm.retrieve_T100(in_mail_address: usr.T100_MAIL_ADDRESS)
**print("*5 after retrieve_T100 *")**
.....
ViewModel.swift
import CloudKit
final class ViewModel: ObservableObject {
private lazy var container = CKContainer(identifier: Config.containerIdentifier)
private lazy var database = container.publicCloudDatabase
.....
func retrieve_T100(in_mail_address:String) {
**print("*2 retrieve_T100:start !!")**
let predicate = NSPredicate(format: "MAIL_ADDRESS == %@", in_mail_address)
let query = CKQuery(recordType: "T100_T_SCHOOL", predicate: predicate)
let queryOperation = CKQueryOperation(query: query)
queryOperation.recordMatchedBlock = { record, result in
switch result {
case .success(let record):
**print("*3 retrieve_T100:(A)MAIL_ADDRESS = \**(record["MAIL_ADDRESS"]!)")
case .failure(let error):
print("*3 retrieve_T100:error = \(error.localizedDescription)")
}
}
queryOperation.queryResultBlock = { result in
switch result {
case .failure(let error):
print("*4 retrieve_T100:(B) Error ...")
print(error.localizedDescription)
case .success:
**print("*4 retrieve_T100:(B) Success ...")**
}
}
database.add(queryOperation)
}
....
report of display after processing
*1 before retrieve_T100 *
*2 retrieve_T100:start !!
*5 after retrieve_T100 *
*3 retrieve_T100:(A)MAIL_ADDRESS = xxxxxxxx
*3 retrieve_T100:(A)MAIL_ADDRESS = xxxxxxxx
*3 retrieve_T100:(A)MAIL_ADDRESS = xxxxxxxx
*4 retrieve_T100:(B) Success ...