天道酬勤,学无止境

core-data

Deleting Relationship Objects with Cascade in Core Data

I'm looking to perform some simple deletion with Core data but just need a bit of advice on this one please. I have a model with Transaction, Name, Event and Date Entities. The Transaction has a link to each of the other Entities. In the app, when a user adds in information to text fields, that gets saved to a 4 tab table view controller. The first tab is using NSFetchedResultController with a fetchRequest on the Transaction Entity. The second tab is using the Name Entity, the third is using the Event and the fourth is using the Date Entity. If I delete an entry from the Transaction tab, it

2021-07-13 12:39:09    分类:问答    ios   objective-c   core-data   cascading

Magical Record save in background

I'm using the Magical Record framework to save user settings. Now, for the first time, I want to save things in a background thread. On Magical Record's github page is an example snippet I don't fully understand: Person *person = ...; [MagicalRecord saveInBackgroundWithBlock:^(NSManagedObjectContext *localContext){ Person *localPerson = [person MR_inContext:localContext]; localPerson.firstName = @"John"; localPerson.lastName = @"Appleseed"; }]; Why is the first line needed? Can't I just completely create the Person in the block? Thank you!

2021-07-13 12:24:09    分类:问答    ios   xcode   multithreading   core-data   magicalrecord

iOS 复合谓词(iOS Compound Predicates)

问题 我正在编写一个具有照片数据库的应用程序。 每张照片都有几个与之关联的标签,该应用程序有一个带有许多切换的搜索页面,允许用户仅根据他们感兴趣的标签搜索照片。 这些标签中的每一个都存储了整数ID,因为它们对应于外部数据库的 ID,所以我试图通过 ID 来查找它们。 所有 ID 字段都已编入索引。 当我编写的谓词变得非常大时,问题就出现了,因为用户可以从许多不同的标签中进行选择进行过滤。 这些标签分为 3 个类别 [出版物、品牌和产品],因此我的查询设置为在一个类别内进行“或”运算,并在这些类别中进行“与”运算。 一个示例查询谓词最终看起来像这样: (parentPublication.publicationId==1 OR parentPublication.publicationId==2 OR parentPublication.publicationId==5) AND (ANY brands.brandId==12 OR ANY brands.brandId==2 OR ANY brands.brandId==0 OR ANY brands.brandId==3 OR ANY brands.brandId==5 OR ANY brands.brandId==6 OR ANY brands.brandId==7) AND (ANY products.productId==2

2021-07-13 04:25:46    分类:技术分享    ios   core-data   nspredicate

Core Data Entity Relationship Does Not Save Between Launches

I am writing an application that has four main entities that are all linked via relationships. Some are one to one, some are one to many. Upon initial load, three of the entities load their data from XML files stored locally to the application and one of the entities downloads an XML from the web and loads its data from it. When the app loads it performs a check to see if the data from each of these files is more recent than what it currently has and, if so, it will replace all current data in that entity with data from the appropriate file. As part of my debug process during writing I have

2021-07-13 04:11:06    分类:问答    iphone   objective-c   cocoa-touch   ios   core-data

我可以设置瞬态属性来获取吗?(Can I set transient properties to fetch?)

问题 我想创建NSFetchRequest并将属性设置为这样获取: request.propertiesToFetch = @[@"a", @"b", @"c"]; 其中a和b存储在 Core Data 数据库中, c是暂时的。 executeFetchRequest:触发错误Invalid keypath c passed to setPropertiesToFetch: 。 但是,如果我取消选中属性c瞬态复选框,一切都会正常工作。 那么真的不可能获取瞬态属性还是我做错了什么? 回答1 是的,这真的不可能。 您无法获取它们,因为它们不存在于持久存储中 - 这就是瞬态的含义。 您可以获取创建派生、瞬态属性的任何持久属性。

2021-07-13 04:02:06    分类:技术分享    ios   objective-c   core-data   nsfetchrequest

How to use @Fetchrequest outside a View

I am trying to move my @fetchrequest property to an auxiliar class, which is not a View, but every time I try to do that, I get a bad instruction error. Can anyone help me? This is a sample of my code: ViewModel: class ViewModel { @FetchRequest(entity: Teste.entity(), sortDescriptors: []) var teste: FetchedResults<Teste> } View: struct ContentView: View { let viewModel: ViewModel init(viewModel: ViewModel) { self.viewModel = viewModel } var body: some View { List(viewModel.teste) { item in // error happens in this line Text(item.id ?? "") } } } Thank you!

2021-07-13 03:53:38    分类:问答    swift   core-data   swiftui   ios13

NSPredicate predicateWithFormat:argumentArray: 只评估第一个参数(NSPredicate predicateWithFormat:argumentArray: Only Evaluating First Argument)

问题 我在使用NSPredicate predicateWithFormat:argumentArray遇到了一些麻烦:。 在本例中, serverIDList是字符串数组。 结果是一个NSManagedObjects数组,它有一个名为“flid”的属性,它是一个字符串。 NSMutableString *predicateString = [[NSMutableString alloc] init]; [predicateString appendString:@"(flid IN %@)"]; [results filterUsingPredicate:[NSPredicate predicateWithFormat:predicateString argumentArray:serverIDList]]; 问题是[NSPredicate predicateWithFormat:predicateString argumentArray:serverIDList]计算结果为“flid IN '2155'”,这只是数组serverIDList的第一个值。 我似乎无法获得评估整个数组的谓词。 这里有什么遗漏吗? 谢谢! 回答1 [NSPredicate predicateWithFormat:@"(flid IN %@)" argumentArray:serverIDList]

2021-07-13 03:48:45    分类:技术分享    ios   core-data   nspredicate

核心数据关系中出现不需要的对象(unwanted objects appearing in core data relationship)

问题 很长的问题---提前感谢您的时间。 保存新的托管对象后,我发现它们被添加到我的核心数据数据库中另一个对象的关系中——我的代码没有调用 setter 方法并且没有反向关系。 我仔细研究了代码并使用日志尽可能地隔离了发生的情况,但是我遇到了无法解释(或修复)的奇怪行为。 进一步来说: 我有一个名为PendingSyncTracker的实体。 它只有一种关系, objectsToSync 。 我还没有在我的代码中添加任何行来调用这个关系的 setter 方法。 这是一对多的关系。 它指向BaseEntity 。 对于“反向”选项,我选择了“无反向关系”。 当我加载特定的表视图时,从服务器下载 3 个对象,然后解析为托管对象并保存。 当表格视图开始加载单元格时,这 3 个对象中的 2 个将神秘地出现在objectsToSync关系中。 我在我的所有代码中都使用了NSLog来准确确定何时可以第一次找到这些对象作为objectsToSync集合的成员。 NSSet *objectsToSync = [[[SyncEngine sharedEngine] fetchClassNamed:@"PendingSyncTracker" withPredicates:nil][0] valueForKey:@"objectsPendingSync"]; NSLog(@

2021-07-13 03:45:24    分类:技术分享    ios   core-data

核心数据:打印实体的内容(Core-Data: Print contents of an entity)

问题 我将如何打印实体(例如客户)的内容? 我希望数据像表格一样,例如实体数据应该像这样打印: 名字 | 姓氏 | 电话号码 | 电子邮件 | 出生日期 我还需要在打印数据之前应用搜索谓词,例如 1984 年以后出生的打印成员 有人能告诉我应该怎么做吗? 谢谢! 回答1 我发现这个链接对我使用核心数据有很大帮助:http://iphoneinaction.manning.com/iphone_in_action/2009/09/core-data-part-3-retrieving-data.html 将实体转换为数组,循环数组并 NSLog 每个结果的数据。 例如: NSEntityDescription *entity = [NSEntityDescription entityForName:@"news" inManagedObjectContext:self.managedObjectContext]; [fetchRequest setEntity:entity]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"url=%@",theUrl]; [fetchRequest setPredicate:predicate]; NSError *error; NSArray *items = [self

2021-07-13 03:44:23    分类:技术分享    objective-c   core-data   printing   entity

NSFetchedResultsController misses updates on merged NSManagedObjectContext

I have two managed object contexts, A and B. These two contexts are kept in sync by merging changes from one to the other whenever they're saved (by monitoring NSManagedObjectContextDidSaveNotification). I've verified that changes to A and B are merging properly. I have an NSFetchedResultsController monitoring updates to B. I find that certain updates to A do not result in the nsfrc firing on B. When a new managed object is created and added to A with the attributes the nsfrc is looking for (on B), the nsfrc fires after B merges changes from the save of A. However, if the managed object

2021-07-13 03:22:34    分类:问答    core-data   merge   nsfetchedresultscontroller   nsmanagedobjectcontext