gpt4 book ai didi

ios - 为什么在一个 View 中有不同的不同上下文?

转载 作者:行者123 更新时间:2023-11-29 01:29:05 25 4
gpt4 key购买 nike

我有一个购物车 Controller 。用户可以将项目添加到他的愿望 list 中。如果我们没有任何愿望 list ,我们应该创建默认的一个。如果我添加,然后删除,然后再次添加,这可以工作 2-3 次。然后我得到错误:

Illegal attempt to establish a relationship 'wishList' between objects in different contexts

class CartViewController: UIViewController, NSFetchedResultsControllerDelegate {
var fetchResultController:NSFetchedResultsController!
var shopItems:[ShopItem] = []
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

override func viewDidLoad() {
self.fetchResultController = NSFetchedResultsController(fetchRequest: fetchRequest,
managedObjectContext: self.appDelegate.managedObjectContext, sectionNameKeyPath: nil, cacheName: nil)
self.fetchResultController.delegate = self
do {
try fetchResultController.performFetch()
self.shopItems = fetchResultController.fetchedObjects as! [ShopItem]
} catch {
fatalError("Failure to save context: \(error)")
}

}
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {

let addToWishListAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Add to wish list", handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
var wishListResults:[WishList] = []
let fetchRequest = NSFetchRequest(entityName: "WishList")
var wishList:WishList
fetchRequest.fetchLimit = 1

//...some other code

wishList = NSEntityDescription.insertNewObjectForEntityForName("WishList", inManagedObjectContext: self.appDelegate.managedObjectContext) as! WishList
wishList.setValue("Default wish list", forKey: "title")
wishList.setValue("My wish list", forKey: "desc")

let shopItem = self.fetchResultController.objectAtIndexPath(indexPath) as! ShopItem

shopItem.setValue(true, forKey: "inWishList")
shopItem.setValue(wishList, forKey: "WishList")

do {
try self.appDelegate.managedObjectContext.save()
} catch {
fatalError("Failure to save context: \(error)")
}
})
}
}

为什么上下文改变了?

最佳答案

您正试图在与创建托管对象上下文不同的线程上创建和保存 CoreData 对象。 UITableViewRowAction 的处理程序提供了一个 block 回调,它将在不同的线程上异步发生。

为了在不同的线程上创建和保存对象,您需要创建另一个 concurrencyType PrivateQueueConcurrencyTypeNSManagedObjectContext,然后从后台线程。

下面是我将如何重写您的 editActionsForRowAtIndexPath 方法(因为每个人都想复制和粘贴,对吧?):

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {

let privateMOC = NSManagedObjectContext(concurrencyType: .PrivateQueueConcurrencyType)
privateMOC.parentContext = self.appDelegate.managedContext
let addToWishListAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Add to wish list", handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
var wishListResults:[WishList] = []
let fetchRequest = NSFetchRequest(entityName: "WishList")
var wishList:WishList
fetchRequest.fetchLimit = 1

//...some other code

wishList = NSEntityDescription.insertNewObjectForEntityForName("WishList", inManagedObjectContext: privateMOC) as! WishList
wishList.setValue("Default wish list", forKey: "title")
wishList.setValue("My wish list", forKey: "desc")

let shopItem = self.fetchResultController.objectAtIndexPath(indexPath) as! ShopItem

shopItem.setValue(true, forKey: "inWishList")
shopItem.setValue(wishList, forKey: "WishList")

do {
try privateMOC.save()
} catch {
fatalError("Failure to save context: \(error)")
}
})
}

此外,确保您正在创建 ManagedObjectContext 以使用 MainQueueConcurrencyType 作为:

var managedObjectContext = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType)

关于ios - 为什么在一个 View 中有不同的不同上下文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33660581/

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