NSFetchedResultsController 不调用 controllerDidChangeContent:更新到非获取的 NSManagedObject 之后 [英] NSFetchedResultsController doesn't call controllerDidChangeContent: after update to non-fetched NSManagedObject
问题描述
- 我填充并
保存:
一个初始的NSManagedObjectContext
- 使用不同的
NSManagedObjectContext
设置一个NSFetchedResultsController
,它过滤boolean
show"属性. - 最后在另一个
NSManagedObjectContext
和save:
上更新show".
- I populate and
save:
an initialNSManagedObjectContext
- setup an
NSFetchedResultsController
with a differentNSManagedObjectContext
, which filters on aboolean
"show" attribute. - Finally update "show" on yet another
NSManagedObjectContext
andsave:
.
我希望这会导致我的 NSFetchedResultsController
调用 NSFetchedResultsControllerDelegate
的 controllerDidChangeContent:
.我从来没有接到过那个电话.NSFetchedResultsController with predicate 忽略从不同 NSManagedObjectContext 合并的变化a href="https://stackoverflow.com/questions/3923826/nsfetchedresultscontroller-with-predicate-ignores-changes-merged-from-different/3927811#3927811">接受的答案表明除了controllerDidChangeContent:
,我应该得到一个 NSManagedObjectContextObjectsDidChangeNotification
,但我也没有收到.
I expect that this should cause my NSFetchedResultsController
to call NSFetchedResultsControllerDelegate
's controllerDidChangeContent:
. I never get that call. NSFetchedResultsController with predicate ignores changes merged from different NSManagedObjectContext's accepted answer indicates that in addition to controllerDidChangeContent:
, I should get an NSManagedObjectContextObjectsDidChangeNotification
, but I don't receive that either.
下面包含一个完整的代码示例,在 github 上.我已经向 Apple 提交了一份雷达报告.
A complete code example is included below and on github. I've filed a radar with Apple.
@interface HJBFoo : NSManagedObject
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSNumber *show;
@end
@interface HJBAppDelegate () <NSFetchedResultsControllerDelegate>
@property (nonatomic, strong) NSPersistentStoreCoordinator *persistentStoreCoordinator;
@property (nonatomic, strong) NSManagedObjectContext *initialManagedObjectContext;
@property (nonatomic, strong) NSManagedObjectContext *fetchedResultsControllerManagedObjectContext;
@property (nonatomic, strong) NSFetchedResultsController *fetchedResultsController;
@end
@implementation HJBAppDelegate
#pragma mark - UIApplicationDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = [UIViewController new];
NSAttributeDescription *nameAttributeDescription = [NSAttributeDescription new];
[nameAttributeDescription setAttributeType:NSStringAttributeType];
[nameAttributeDescription setIndexed:NO];
[nameAttributeDescription setOptional:NO];
[nameAttributeDescription setName:@"name"];
NSAttributeDescription *showAttributeDescription = [NSAttributeDescription new];
[showAttributeDescription setAttributeType:NSBooleanAttributeType];
[showAttributeDescription setIndexed:YES];
[showAttributeDescription setOptional:NO];
[showAttributeDescription setName:@"show"];
NSEntityDescription *fooEntityDescription = [NSEntityDescription new];
[fooEntityDescription setManagedObjectClassName:@"HJBFoo"];
[fooEntityDescription setName:@"HJBFoo"];
[fooEntityDescription setProperties:@[
nameAttributeDescription,
showAttributeDescription,
]];
NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel new];
[managedObjectModel setEntities:@[
fooEntityDescription,
]];
self.persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel];
NSError *error = nil;
if ([self.persistentStoreCoordinator addPersistentStoreWithType:NSInMemoryStoreType
configuration:nil
URL:nil
options:nil
error:&error]) {
self.initialManagedObjectContext = [NSManagedObjectContext new];
[self.initialManagedObjectContext setPersistentStoreCoordinator:self.persistentStoreCoordinator];
HJBFoo *foo1 = [NSEntityDescription insertNewObjectForEntityForName:@"HJBFoo"
inManagedObjectContext:self.initialManagedObjectContext];
foo1.name = @"1";
foo1.show = @YES;
HJBFoo *foo2 = [NSEntityDescription insertNewObjectForEntityForName:@"HJBFoo"
inManagedObjectContext:self.initialManagedObjectContext];
foo2.name = @"2";
foo2.show = @NO;
error = nil;
if ([self.initialManagedObjectContext save:&error]) {
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"HJBFoo"];
[fetchRequest setReturnsObjectsAsFaults:NO];
error = nil;
NSArray *initialFoos = [self.initialManagedObjectContext executeFetchRequest:fetchRequest
error:&error];
if (initialFoos) {
NSLog(@"Initial: %@", initialFoos);
self.fetchedResultsControllerManagedObjectContext = [NSManagedObjectContext new];
[self.fetchedResultsControllerManagedObjectContext setPersistentStoreCoordinator:self.persistentStoreCoordinator];
NSFetchRequest *shownFetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"HJBFoo"];
[shownFetchRequest setPredicate:[NSPredicate predicateWithFormat:@"show == YES"]];
[shownFetchRequest setSortDescriptors:@[
[NSSortDescriptor sortDescriptorWithKey:@"name"
ascending:YES],
]];
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:shownFetchRequest
managedObjectContext:self.fetchedResultsControllerManagedObjectContext
sectionNameKeyPath:nil
cacheName:nil];
self.fetchedResultsController.delegate = self;
error = nil;
if ([self.fetchedResultsController performFetch:&error]) {
NSLog(@"Initial fetchedObjects: %@", [self.fetchedResultsController fetchedObjects]);
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(managedObjectContextDidSave:)
name:NSManagedObjectContextDidSaveNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(managedObjectContext2ObjectsDidChange:)
name:NSManagedObjectContextObjectsDidChangeNotification
object:self.fetchedResultsControllerManagedObjectContext];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC),
dispatch_get_main_queue(),
^(void){
NSManagedObjectContext *managedObjectContext3 = [NSManagedObjectContext new];
[managedObjectContext3 setPersistentStoreCoordinator:self.persistentStoreCoordinator];
NSFetchRequest *foo2FetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"HJBFoo"];
[foo2FetchRequest setFetchLimit:1];
[foo2FetchRequest setPredicate:[NSPredicate predicateWithFormat:@"name == %@",
@"2"]];
NSError *editingError = nil;
NSArray *editingFoos = [managedObjectContext3 executeFetchRequest:foo2FetchRequest
error:&editingError];
if (editingFoos) {
HJBFoo *editingFoo2 = [editingFoos objectAtIndex:0];
editingFoo2.show = @YES;
editingError = nil;
if ([managedObjectContext3 save:&editingError]) {
NSLog(@"Save succeeded. Expected (in order) managedObjectContextDidSave, controllerDidChangeContent, managedObjectContext2ObjectsDidChange");
} else {
NSLog(@"Editing save failed: %@ %@", [error localizedDescription], [error userInfo]);
}
} else {
NSLog(@"Editing fetch failed: %@ %@", [error localizedDescription], [error userInfo]);
}
});
} else {
NSLog(@"Failed initial fetch: %@ %@", [error localizedDescription], [error userInfo]);
}
} else {
NSLog(@"Failed to performFetch: %@ %@", [error localizedDescription], [error userInfo]);
}
} else {
NSLog(@"Failed to save initial state: %@ %@", [error localizedDescription], [error userInfo]);
}
} else {
NSLog(@"Failed to add persistent store: %@ %@", [error localizedDescription], [error userInfo]);
}
[self.window makeKeyAndVisible];
return YES;
}
#pragma mark - NSFetchedResultsControllerDelegate
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
NSLog(@"controllerDidChangeContent: %@",
[self.fetchedResultsController fetchedObjects]);
}
#pragma mark - notifications
- (void)managedObjectContextDidSave:(NSNotification *)notification {
NSManagedObjectContext *managedObjectContext = [notification object];
if (([managedObjectContext persistentStoreCoordinator] == self.persistentStoreCoordinator) &&
(managedObjectContext != self.fetchedResultsControllerManagedObjectContext)) {
NSLog(@"managedObjectContextDidSave: %@", notification);
[self.fetchedResultsControllerManagedObjectContext mergeChangesFromContextDidSaveNotification:notification];
}
}
- (void)managedObjectContext2ObjectsDidChange:(NSNotification *)notification {
NSLog(@"managedObjectContext2ObjectsDidChange: %@", notification);
}
@end
@implementation HJBFoo
@dynamic name;
@dynamic show;
@end
推荐答案
在我看来,应用 带有谓词的 NSFetchedResultsController 忽略从不同 NSManagedObjectContext 合并的更改 也可以解决您的问题.您的 managedObjectContextDidSave
方法将如下所示:
It seems to me that applying the fix/workaround from NSFetchedResultsController with predicate ignores changes merged from different NSManagedObjectContext solves your problem as well. Your managedObjectContextDidSave
method would then look like this:
- (void)managedObjectContextDidSave:(NSNotification *)notification {
NSManagedObjectContext *managedObjectContext = [notification object];
if (([managedObjectContext persistentStoreCoordinator] == self.persistentStoreCoordinator) &&
(managedObjectContext != self.fetchedResultsControllerManagedObjectContext)) {
NSLog(@"managedObjectContextDidSave: %@", notification);
// Fix/workaround from https://stackoverflow.com/questions/3923826/nsfetchedresultscontroller-with-predicate-ignores-changes-merged-from-different/3927811#3927811
for(NSManagedObject *object in [[notification userInfo] objectForKey:NSUpdatedObjectsKey]) {
[[self.fetchedResultsControllerManagedObjectContext objectWithID:[object objectID]] willAccessValueForKey:nil];
}
[self.fetchedResultsControllerManagedObjectContext mergeChangesFromContextDidSaveNotification:notification];
}
}
并且 NSLog 输出看起来如预期:
and the NSLog output looks as expected:
Initial: (
"<HJBFoo: 0xaa19670> (entity: HJBFoo; id: 0xaa1afd0 <x-coredata://07E97098-E32D-45F6-9AB4-F9DAB9B0AC1A/HJBFoo/p2> ; data: {
name = 1;
show = 1;
})",
"<HJBFoo: 0xaa1a200> (entity: HJBFoo; id: 0xaa1af50 <x-coredata://07E97098-E32D-45F6-9AB4-F9DAB9B0AC1A/HJBFoo/p1> ; data: {
name = 2;
show = 0;
})"
)
Initial fetchedObjects: (
"<HJBFoo: 0x74613f0> (entity: HJBFoo; id: 0xaa1afd0 <x-coredata://07E97098-E32D-45F6-9AB4-F9DAB9B0AC1A/HJBFoo/p2> ; data: <fault>)"
)
managedObjectContextDidSave: NSConcreteNotification 0xaa1f480 {name = NSManagingContextDidSaveChangesNotification; object = <NSManagedObjectContext: 0xaa1ed90>; userInfo = {
inserted = "{(
)}";
updated = "{(
<HJBFoo: 0xaa1f2d0> (entity: HJBFoo; id: 0xaa1af50 <x-coredata://07E97098-E32D-45F6-9AB4-F9DAB9B0AC1A/HJBFoo/p1> ; data: {
name = 2;
show = 1;
})
)}";
}}
Save succeeded. Expected (in order) managedObjectContextDidSave, controllerDidChangeContent, managedObjectContext2ObjectsDidChange
controllerDidChangeContent: (
"<HJBFoo: 0x74613f0> (entity: HJBFoo; id: 0xaa1afd0 <x-coredata://07E97098-E32D-45F6-9AB4-F9DAB9B0AC1A/HJBFoo/p2> ; data: {
name = 1;
show = 1;
})",
"<HJBFoo: 0xaa1f9c0> (entity: HJBFoo; id: 0xaa1af50 <x-coredata://07E97098-E32D-45F6-9AB4-F9DAB9B0AC1A/HJBFoo/p1> ; data: {
name = 2;
show = 1;
})"
)
managedObjectContext2ObjectsDidChange: NSConcreteNotification 0xaa1fbb0 {name = NSObjectsChangedInManagingContextNotification; object = <NSManagedObjectContext: 0x745fa50>; userInfo = {
managedObjectContext = "<NSManagedObjectContext: 0x745fa50>";
refreshed = "{(
<HJBFoo: 0xaa1f9c0> (entity: HJBFoo; id: 0xaa1af50 <x-coredata://07E97098-E32D-45F6-9AB4-F9DAB9B0AC1A/HJBFoo/p1> ; data: {
name = 2;
show = 1;
})
)}";
}}
所以发生了以下事情:
- 在背景"上下文
managedObjectContext3
中进行更改并保存. - 您收到一个
NSManagedObjectContextDidSaveNotification
并且managedObjectContextDidSave:
被调用.此通知包含在后台上下文中更新的对象. 打电话
- Changes are made in the "background" context
managedObjectContext3
and saved. - You receive a
NSManagedObjectContextDidSaveNotification
andmanagedObjectContextDidSave:
is called. This notification contains the object that was updated in the background context. Calling
[self.fetchedResultsControllerManagedObjectContext mergeChangesFromContextDidSaveNotification:notification];
现在不会什么都不做,因为更新的对象还没有加载到fetchedResultsControllerManagedObjectContext
或者是一个错误.特别是,此上下文不会发布 NSManagedObjectContextObjectsDidChangeNotification
并且获取的结果控制器不会更新.
now would not do anything, because the updated object has not been loaded into the fetchedResultsControllerManagedObjectContext
or is a fault. In particular, this context would not post a NSManagedObjectContextObjectsDidChangeNotification
and the fetched results controller would not update.
打电话
[self.fetchedResultsControllerManagedObjectContext mergeChangesFromContextDidSaveNotification:notification];
之后实际更新fetchedResultsControllerManagedObjectContext
中的对象.
after that actually updates the objects in the fetchedResultsControllerManagedObjectContext
.
这篇关于NSFetchedResultsController 不调用 controllerDidChangeContent:更新到非获取的 NSManagedObject 之后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!