我尝试从iCloud保存和检索阵列。然后,我要打印结果并将结果显示在标签中。
在模拟器中,结果会立即打印出来,但标签上的文本只是在aprox 15秒后显示,不明白为什么会出现这种延迟!
无论如何,我的代码在模拟器中(或在直接链接到Xcode的设备中)工作,但是当我将应用程序发送到“ testflight”并从那里下载时,它不再在我的标签中显示结果。
我的测试人员已经在其他设备上进行了尝试,但没有结果:将应用发送给试飞后,标签上没有任何显示
import UIKit
import CloudKit
class ViewController: UIViewController {
struct Structure: Codable {
var a, b, c, d : String
var e : Double
}
var var1 = [
Structure(a: "-1", b: "111", c: "aaa", d: "2017-01-21 05:55:55", e: 1),
Structure(a: "111.1", b: "-1", c: "AAAAAA", d: "2018-04-27 07:54:25", e: 11)]
var var2 = [
Structure(a: "", b: "", c: "", d: "", e: 0),
Structure(a: "", b: "", c: "", d: "", e: 0)]
@IBOutlet weak var Label: UILabel!
@IBAction func Save(_ sender: Any) {
// Delete old record
let query = CKQuery(recordType: "Structure", predicate: NSPredicate(value: true))
CKContainer.default().privateCloudDatabase.perform(query, inZoneWith: nil) { (records, error) in
if error == nil {
for record in records! {
CKContainer.default().privateCloudDatabase.delete(withRecordID: record.recordID, completionHandler: { (recordId, error) in })
}
print("Deleted record")
}
}
// Save var1
let myRecord = CKRecord(recordType: "Structure")
if let data = try? JSONEncoder().encode(var1) {
myRecord["Content1"] = data
}
CKContainer.default().privateCloudDatabase.save(myRecord) { (record, error) in
guard record != nil else { return }
print("saved var1")
}
}
@IBAction func Load(_ sender: Any) {
let query = CKQuery(recordType: "Structure", predicate: NSPredicate(value: true))
CKContainer.default().privateCloudDatabase.perform(query, inZoneWith: nil) { (records, error) in
records?.forEach({ (record) in
let content = record.value(forKey: "Content1")
//convert data to var2
if let lp = content as? Data{
if let valuefromicloud = try? JSONDecoder().decode(Array.self, from: lp) as [Structure]{
self.var2 = valuefromicloud
}
}
//The next line print result instantly
print("Data from iCloud: \(self.var2[0].b)")
//The next line show result after aprox 15 sec delay, why this delay???
self.Label.text = "Data from iCloud: \(self.var2[0].b)"
})
}}
}
所以,我有三个问题:
1:为什么会立即打印结果,但是标签需要大约15秒才能在模拟器中更新?
2:为什么我的应用程序从测试飞行中下载后,我的应用程序不再更新他的标签?
3:您能给我一个可以从iCloud中保存/提取数组的工作代码的例子吗?