【发布时间】:2023-04-05 01:03:01
【问题描述】:
这是我所拥有的:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"NoteObject" inManagedObjectContext:appDelegate.managedObjectContext];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(parentNoteId == %@) AND (noteId!=rootNoteId)", appDelegate.currentNoteId];
[fetchRequest setPredicate:predicate];
NSError *error;
[appDelegate.arrayOfNotes setArray:[appDelegate.managedObjectContext executeFetchRequest:fetchRequest error:&error]];
NoteObject *note=[appDelegate.arrayOfNotes objectAtIndex:0];
[note.arrayOfTags addObject:someObject];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
if (![context save:&error]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}
提取工作正常,将对象添加到arrayOfTags 工作并反映在 UI 中。但是,当我退出应用程序并返回时,arrayOfTags 缺少我添加的那个(但它有另外两个,所以我知道数组工作正常)。由于某种原因,它没有保存。
(NoteObject 是 NSManagedObject 的子类,arrayOfTags 是实体的可转换属性。)
我在这里做错了吗?
编辑:这是我添加新注释的方法,即使使用 arrayOfTags 也可以很好地保存,当我退出应用程序并返回时,所有内容都会保存。
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSEntityDescription *noteEntity = [[appDelegate.managedObjectModel entitiesByName] objectForKey:@"NoteObject"];
NoteObject *tempNote = [[NoteObject alloc] initWithEntity:noteEntity insertIntoManagedObjectContext:context];
[tempNote.arrayOfTags addObject:@"tag1"];
NSError *error;
if (![context save:&error]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}
只有当我进行更改时,它才没有正确保存..
【问题讨论】:
-
只是一个字符串..我还不确定,但我认为我的问题可能是我没有使用 fetchedResultsController
-
这似乎更有可能是您的 arrayOfTags 实现中的问题 - 您能否提供更多详细信息?
标签: objective-c core-data