gpt4 book ai didi

ios - 从 CloudKit 获取并保存到 CoreData

转载 作者:行者123 更新时间:2023-11-30 13:45:06 24 4
gpt4 key购买 nike

这将是一个非常菜鸟的问题,但我正在尝试让我的应用程序从 CloudKit 下载数据,然后将其保存到 CoreData。

当我运行此类代码时,出现以下错误。我对 CoreData 实在是菜鸟,所以这对我来说很难理解。我认为这与我发送请求的方式有关,但我不确定应该如何解决它。我得到的错误是:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'recordChangeSnapshot:forObjectID:: global ID may not be temporary when recording '

大家有什么想法吗?

import UIKit
import CloudKit
import CoreData

class Start: UIViewController {

var classroomEN: String?
var classroomTC: String?
var classroomSC: String?

var videos = [NSManagedObject]()

override func viewDidLoad() {

fetchData()
fetchDataTC()
}

func fetchData() {

//added to fetch data from CloudKit
let container = CKContainer.defaultContainer()
let publicData = container.publicCloudDatabase

let predicate = NSPredicate(value: true)

let queryEN = CKQuery(recordType: "ClassroomFAQEN", predicate: predicate)
let queryTC = CKQuery(recordType: "ClassroomFAQTC", predicate: predicate)

queryEN.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
queryTC.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]

publicData.performQuery(queryEN, inZoneWithID: nil) { results, error in

if error == nil { // There is no error

for entry in results! {
let newFAQ = classFAQ()
newFAQ.title = entry["Title"] as! String
newFAQ.content = entry["Content"] as! String
if entry["Picture"] != nil {
print("There is no picture")
newFAQ.picture = entry["Picture"] as! String
}
if entry["Video"] != nil {
print("There is no video")
newFAQ.video = entry["Video"] as! String
}

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
let entity = NSEntityDescription.entityForName("ClassroomFAQEN", inManagedObjectContext:managedContext)
let video = NSManagedObject(entity: entity!, insertIntoManagedObjectContext: managedContext)
video.setValue(newFAQ.title, forKey: "title")
video.setValue(newFAQ.content, forKey: "content")
video.setValue(newFAQ.picture, forKey: "picture")
video.setValue(newFAQ.video, forKey: "video")

do {
try video.managedObjectContext!.save()
self.videos.append(video)
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
}

dispatch_async(dispatch_get_main_queue(), { () -> Void in
print("Reloading data in tableView")
self.fetchDataTC()
})
}
}
else {
print(error)
}
}
}

func fetchDataTC() {

//added to fetch data from CloudKit
let container = CKContainer.defaultContainer()
let publicData = container.publicCloudDatabase

let predicate = NSPredicate(value: true)

let queryEN = CKQuery(recordType: "ClassroomFAQEN", predicate: predicate)
let queryTC = CKQuery(recordType: "ClassroomFAQTC", predicate: predicate)

queryEN.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
queryTC.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]

publicData.performQuery(queryTC, inZoneWithID: nil) { results, error in

if error == nil { // There is no error

for entry in results! {
let newFAQ = classFAQ()
newFAQ.title = entry["Title"] as! String
newFAQ.content = entry["Content"] as! String
if entry["Picture"] != nil {
print("There is no picture")
newFAQ.picture = entry["Picture"] as! String
}
if entry["Video"] != nil {
print("There is no video")
newFAQ.video = entry["Video"] as! String
}

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
let entity = NSEntityDescription.entityForName("ClassroomFAQTC", inManagedObjectContext:managedContext)
let video = NSManagedObject(entity: entity!, insertIntoManagedObjectContext: managedContext)
video.setValue(newFAQ.title, forKey: "title")
video.setValue(newFAQ.content, forKey: "content")
video.setValue(newFAQ.picture, forKey: "picture")
video.setValue(newFAQ.video, forKey: "video")

do {
try video.managedObjectContext!.save()
self.videos.append(video)
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
}

dispatch_async(dispatch_get_main_queue(), { () -> Void in
print("Reloading data in tableView")
})
}
}
else {
print(error)
}
}

}

最佳答案

您可以使用 isMainThread 来确定您是否在后台线程上..或者您可以直接编写这样的代码,这将始终确保它在主线程中:-

    dispatch_async(dispatch_get_main_queue(), { () -> Void in
do {
try video.managedObjectContext!.save()
self.videos.append(video)
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
}
})

关于ios - 从 CloudKit 获取并保存到 CoreData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35052849/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com