gpt4 book ai didi

ios - NSFetchedResultsController 在后台线程上使用核心数据

转载 作者:搜寻专家 更新时间:2023-11-01 07:32:44 25 4
gpt4 key购买 nike

我知道 Stack Overflow 上已经有很多与此相关的问题。我尝试了各种方法,现在我什至不确定什么是对的,什么不是:S

我有一个相当标准的问题,我希望在一个单独的线程上更新核心数据,并在更新/添加记录时更新 UI( TableView )。

根据我的理解,我需要有两个 NSManagedObjectContext 实例

所以我已经设置了我的 AppDelegate 方法:

 lazy var managedObjectContext: NSManagedObjectContext? = {
// Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) This property is optional since there are legitimate error conditions that could cause the creation of the context to fail.
let coordinator = self.persistentStoreCoordinator
if coordinator == nil {
return nil
}
var managedObjectContext = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType)
managedObjectContext.persistentStoreCoordinator = coordinator

NSNotificationCenter.defaultCenter().addObserver(self, selector: "contextDidSave:", name: NSManagedObjectContextDidSaveNotification, object: nil)
return managedObjectContext
}()

// MARK: - Core Data Saving support
func contextDidSave(notification: NSNotification){
let sender = notification.object as! NSManagedObjectContext
if sender !== self.managedObjectContext{
self.managedObjectContext!.mergeChangesFromContextDidSaveNotification(notification)
}
}

现在应用程序在到达这一行时崩溃了

 self.managedObjectContext!.mergeChangesFromContextDidSaveNotification(notification)

我的 View Controller 设置如下:

    let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext

lazy var fetchedResultsController: NSFetchedResultsController = {
let workFetchRequest = NSFetchRequest(entityName: "Work")
let primarySortDescriptor = NSSortDescriptor(key: "createdDate", ascending: true)
let secondarySortDescriptor = NSSortDescriptor(key: "town", ascending: true)
workFetchRequest.sortDescriptors = [primarySortDescriptor, secondarySortDescriptor]

let frc = NSFetchedResultsController(
fetchRequest: workFetchRequest,
managedObjectContext: self.managedObjectContext!,
sectionNameKeyPath: "createdDate",
cacheName: nil)

frc.delegate = self

return frc
}()


// MARK: NSFetchedResultsControllerDelegate methods
func controllerWillChangeContent(controller: NSFetchedResultsController) {
self.tblJobs.beginUpdates()
}

func controller(
controller: NSFetchedResultsController,
didChangeObject anObject: AnyObject,
atIndexPath indexPath: NSIndexPath?,
forChangeType type: NSFetchedResultsChangeType,
newIndexPath: NSIndexPath?) {

switch type {
case NSFetchedResultsChangeType.Insert:

if let insertIndexPath = newIndexPath {
self.tblJobs.insertRowsAtIndexPaths([insertIndexPath], withRowAnimation: UITableViewRowAnimation.Fade)
}
case NSFetchedResultsChangeType.Delete:

if let deleteIndexPath = indexPath {
self.tblJobs.deleteRowsAtIndexPaths([deleteIndexPath], withRowAnimation: UITableViewRowAnimation.Fade)
}
case NSFetchedResultsChangeType.Update:

if let updateIndexPath = indexPath {
let cell = self.tblJobs.dequeueReusableCellWithIdentifier(
"JobCell", forIndexPath: updateIndexPath)
as! JobTableViewCell

let workItem = self.fetchedResultsController.objectAtIndexPath(updateIndexPath) as? Work

cell.lblAddress1?.text = workItem!.propertyName + " " + workItem!.propertyNumber + " " + workItem!.street
cell.lblAddress2?.text = workItem!.town + " " + workItem!.locality + " " + workItem!.postcode
}
case NSFetchedResultsChangeType.Move:

if let deleteIndexPath = indexPath {
self.tblJobs.deleteRowsAtIndexPaths([deleteIndexPath], withRowAnimation: UITableViewRowAnimation.Fade)
}


if let insertIndexPath = newIndexPath {
self.tblJobs.insertRowsAtIndexPaths([insertIndexPath], withRowAnimation: UITableViewRowAnimation.Fade)
}
}
}

func controller(
controller: NSFetchedResultsController,
didChangeSection sectionInfo: NSFetchedResultsSectionInfo,
atIndex sectionIndex: Int,
forChangeType type: NSFetchedResultsChangeType) {

switch type {
case .Insert:
let sectionIndexSet = NSIndexSet(index: sectionIndex)
self.tblJobs.insertSections(sectionIndexSet, withRowAnimation: UITableViewRowAnimation.Fade)
case .Delete:
let sectionIndexSet = NSIndexSet(index: sectionIndex)
self.tblJobs.deleteSections(sectionIndexSet, withRowAnimation: UITableViewRowAnimation.Fade)
default:
""
}
}

同样,我不确定我在这里调用 managedObjectContext 的方式是否正确。

最后是我的网络课,我意识到很多这需要抽象出来,但我只是想让它先工作。请注意,我正在使用 dispatch_async 来利用一个单独的线程,与使用多线程相关的所有内容都指向使用它,但现在我不确定在使用 NSManagedObjectContext 的上下文中是否需要它

let lockQueue = dispatch_queue_create("com.app.LockQueue", nil)
let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext

func makeRequest(url : String, params : [String : String]?, completionHandler: (responseObject: JSON?, error: NSError?) -> ()) -> Request? {

return Alamofire.request(.GET, url, parameters: params, encoding: .URL)
.responseString { request, response, responseBody, error in completionHandler(
responseObject:
{
// JSON to return
var json : JSON?

if let response = responseBody {
// Parse the response to NSData
if let data = (response as NSString).dataUsingEncoding(NSUTF8StringEncoding) {
json = JSON(data: data)
}
}

return json

}(), error: error)
}
}

func fetchItems(completion: (NSError?) -> Void) {
self.makeRequest("http://localhost/jobs.json", params: nil) { json, error in

var tempContext: NSManagedObjectContext = NSManagedObjectContext(concurrencyType: .PrivateQueueConcurrencyType)
tempContext.parentContext = self.managedObjectContext


tempContext.performBlock({

dispatch_async(self.lockQueue) {


let entity = "Work"

if let obj = json {

for (index, object) in obj {

var request = NSFetchRequest(entityName: entity)
request.predicate = NSPredicate(format: "id = %@", object["Id"].stringValue)
var error: NSError?
if let entities = tempContext.executeFetchRequest(
request,
error: &error
) as? [NSManagedObject] {
if entities.count != 0{
println("found an existing record...")
for entity in entities {
/*TODO: Change this to call an external function to update all entities*/
entity.setValue(object["Town"].stringValue, forKey: "Town")

}

tempContext.save(nil)
}else{
println("Creating a new entity")

Work.createInManagedObjectContext(tempContext,
id: object["Id"].stringValue,
flatNumber: object["FlatNumber"].stringValue,
propertyName: object["PropertyName"].stringValue,
propertyNumber: object["PropertyNumber"].stringValue,
street: object["Street"].stringValue,
locality: object["Locality"].stringValue,
town: object["Town"].stringValue,
postcode: object["Postcode"].stringValue,
createdDate: object["CreatedDate"].stringValue

)

}
}

if !tempContext.save(&error) {
NSLog("Unresolved error \(error), \(error!.userInfo)")
abort()

}

}
}

}

var error: NSError? = nil
if tempContext.hasChanges && !tempContext.save(&error) {
NSLog("error: %@\n UserInfo: %@\n", error!, error!.userInfo!)
}
else {
println("Error?")
}

})

completion(error)

}
}

我遇到的问题是该应用程序没有保存记录,并且在插入它们时随机掉落(我正在尝试复制这种情况,我会更新一些进一步的信息)以及当我正在更新 tableview 我可以看到后台操作正在运行,但表只是没有更新。

我意识到另一个用户有类似的问题,结果证明更多的是他的 NSFetchedResultsController 的问题。但我感觉我的后台线程设置不正确。

最佳答案

不要创建自己的队列并发送给它。通过调用 tempContext.performBlock,您要求上下文为您在其自己的后台队列上执行操作。因此,当您在该 block 内进行调度时,您就违反了契约(Contract)。

您也不需要通知和合并,因为您将父级设置为您的主线程上下文,因此更改将自动保存。但是,当您保存了临时上下文后,您需要保存主上下文,因为它会因您刚刚所做的更改而变脏(这实际上是将数据保存到持久存储并因此保存到磁盘)。

当触发主上下文保存时,如果您要匹配每个批量保存操作,您应该在 performBlockAndWait: 中执行此操作。

关于ios - NSFetchedResultsController 在后台线程上使用核心数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31537893/

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