use*_*617 4 constraints core-data ios swift
我正在尝试使用新的实体约束检查器在核心数据中设置约束(以使项目的名称唯一)。我读过的所有内容都表明这非常简单 - 设置约束并处理错误。我没有收到任何错误,并且可以根据需要多次添加相同的条目。
该应用程序确实需要 IOS 9.0,Xcode 工具要求设置为 7.0
约束category1Name 是一个字符串。
我的 addItem 代码是:
func addNewRecord() {
//check to be sure the entry is not empty
if (categoryTextField.text == "") {
//prompt requiring a name
let ac = UIAlertController(title: nil, message: "Name Required", preferredStyle: .Alert)
ac.addAction(UIAlertAction(title: "Ok", style: .Default, handler: nil))
self.presentViewController(ac, animated: true, completion: nil)
} else {
let newManagedObject = NSEntityDescription.insertNewObjectForEntityForName("Category1", inManagedObjectContext: kAppDelegate.managedObjectContext) as! Category1
newManagedObject.category1Name = categoryTextField.text
newManagedObject.category1Description = categoryTextView.text
//bunch more items...
//save it
kAppDelegate.saveContext()
makeEntryFieldsEnabledNO()
performSegueWithIdentifier("unwindToCategoriesTableViewController", sender: self)
}//if else
}//addNewRecord
Run Code Online (Sandbox Code Playgroud)
AppDelegate 保存是标准的:
func saveContext () {
if managedObjectContext.hasChanges {
do {
try managedObjectContext.save()
} catch {
//insert your standard error alert stuff here
let nserror = error as NSError
print("From the print line: Unresolved error \(nserror), \(nserror.userInfo)")
abort()
}//do catch
}//if moc
}//saveContext
Run Code Online (Sandbox Code Playgroud)
这是核心数据约束:
此应用程序已启用 iCloud。
ManagedObjectContext 合并策略设置为 NSMergeByPropertyObjectTrumpMergePolicy
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
var managedObjectContext = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType)
managedObjectContext.persistentStoreCoordinator = coordinator
managedObjectContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
return managedObjectContext
}()//var managedObjectContext
Run Code Online (Sandbox Code Playgroud)
任何指导将不胜感激。
Apple 似乎终于解决了 Xcode 的疯狂问题,即您在数据模型文件中所做的更改实际上并没有改变。
\n抛开这一点,当前的公式似乎是:
\n在你的核心数据单例中......
\n container = NSPersistentContainer(name: _nom)\n \n // during development, right HERE likely delete the sql database file\n // and start fresh, as described here stackoverflow.com/a/60040554/294884\n \n container.loadPersistentStores { storeDescription, error in\n if let error = error {\n print("\\n ERROR LOADING STORES! \\(error) \\n")\n }\n else {\n print("\\n STORES LOADED! \\(storeDescription) \\n")\n }\n \n self.container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy\n self.container.viewContext.automaticallyMergesChangesFromParent = true\n }\nRun Code Online (Sandbox Code Playgroud)\n然后在你的数据模型文件中
\n然后当你添加新数据时,你必须
\nperform容易吧?
\n所以像
\nlet pm = core.container.newBackgroundContext()\npm.perform {\n for onePerson in someNewData {\n ... create your new CDPerson entity ...\n }\n pm.bake()\n}\nRun Code Online (Sandbox Code Playgroud)\n请注意,烘焙例程位于执行块内,
\n它看起来像这样:
\nfunc bake() {\n self.performAndWait {\n if self.hasChanges {\n do {\n try self.save()\n }\n catch {\n print("bake disaster type 1 \\(error)")\n }\n }\n \n // OPTIONALLY, SEE BELOW\n if core.container.viewContext.hasChanges {\n do {\n try core.container.viewContext.save()\n }\n catch {\n print("bake disaster type 2 \\(error)")\n }\n }\n // OPTIONALLY, SEE BELOW\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n需要明确的是,请注意pm.bake函数中的 ... bake(),self前半部分中的 确实是newBackgroundContext为执行中的循环创建的。
automaticallyMergesChangesFromParent如果你“完成上面长列表中的所有事情”,现在似乎工作得很好。
\xe2\x80\xa2 在上面的烘焙中,添加几行打印行以查看保存到 viewContext 的内容。你会发现什么都没有被保存。这一切都是由子/引擎中的任何关系正确完成的
\n\xe2\x80\xa2 所以事实上,你可以省略这段代码。你所要做的就是
\nfunc bake() {\n self.performAndWait {\n if self.hasChanges {\n do {\n try self.save()\n }\n catch {\n print("bake disaster type 1 \\(error)")\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
4945 次 |
| 最近记录: |