我遇到了一个问题:并非所有项目都从 iOS设备上的CoreData
托管对象上下文中删除。可能会发生这种情况,因为传递给clear()
函数的项之一未被删除,并且保留在托管对象上下文中。我的Swift代码如下所示:
func clear(items: [Item]) {
for item in items {
self.removeItem(id: NSUUID(uuidString: item.id)!);
}
do {
try self.managedObjectContext!.save();
} catch let error as NSError {
}
}
func removeItem(id: NSUUID) {
let fetchRequest = NSFetchRequest<ItemEntity>(entityName: self.itemEntityKey);
fetchRequest.predicate = NSPredicate(format: self.idKey + " == %@", id.uuidString);
do {
let result = try self.managedObjectContext!.fetch(fetchRequest);
for resultItem in result {
self.managedObjectContext!.delete(resultItem);
}
} catch let error as NSError {
}
}
当在夜间收到远程通知时,代码在后台执行。我做错了什么?