ios - 在核心数据 swift 中使用 one -> may 关系保存数据集合

标签 ios swift core-data entity persistence

我有这样的一对多关系: core data bullshit

目标是拥有一个列表,该列表与该列表中的项目集合有关系。另一个堆栈溢出帖子告诉我,我需要这种关系来存储项目列表,但我不知道如何将项目添加到特定列表。我也不知道如何保存它们。

任何建议、代码示例或教程都会非常有帮助。谢谢。

最佳答案

首先创建“List”对象,然后需要创建“ItemList”对象,将它们添加到“List”对象并保存:

//Create List (if you didn't created it already).    
List *yourListObject = [NSEntityDescription insertNewObjectForEntityForName:@"List" inManagedObjectContext:yourManagedContext];

//Create as many ListItems as you need
ItemList *item1 = [NSEntityDescription insertNewObjectForEntityForName:@"ItemList" inManagedObjectContext:yourManagedContext];

ItemList *item2 = [NSEntityDescription insertNewObjectForEntityForName:@"ItemList" inManagedObjectContext:yourManagedContext];

//and so on ...
//You can add then one by one

[yourListObject addListItemObject:item1];
[yourListObject addListItemObject:item2];

//OR all at once
NSSet *itemsSet = [NSSet setWithObjects:item1,item2, nil];
[yourListObject addListItems: itemsSet];

//Save
NSError *saveError = nil;
[yourManagedContext save:&saveError];

关于ios - 在核心数据 swift 中使用 one -> may 关系保存数据集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34180821/

相关文章:

ios - FileHandle 在 iOS 中不释放内存

ios - 在 CoreData 中存储图像数组

ios - 远程通知的后台任务在短时间后暂停

iphone - 被触摸时更新 View

ios - 调整 iOS 11 上的 UI 导航

ios - 在实体中找不到键路径 <transientproperty>

ios - NSFetchedResultsControllerDelegate 在错误的 indexPath 上动画删除

iOS8 : How to load xib from Dynamic Framework

ios - 使用 createOrUpdate 的 Realm 部分更新不起作用

swift - swift 中的 UICollectionview 页脚