开发者

Confused about how fetchResultController works with MagicalRecord

开发者 https://www.devze.com 2023-04-08 08:59 出处:网络
I have a NSOperation subclass,this is the main method: (void)main { NSAutoreleasePool *Pool = [[NSAutoreleasePool alloc] init];

I have a NSOperation subclass,this is the main method:

(void)main
{
    NSAutoreleasePool *Pool = [[NSAutoreleasePool alloc] init];

    managedObjectContext = [NSManagedObjectContext contextThatNotifiesDefaultContextOnMainThread];

    Message *message = (Message *) [managedObjectContext objectWithID:self.messageID];
    message.status = [NSNumber numberWithInt:SKMessageSendStateStart];
    [message save];
    [self send];
    [self finish];
    [Pool drain];
}

I define the fetchResultController and defaultContext like this:

(BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [MagicalRecordHelpers setupCoreDataStackWithStoreNamed:@"Shark"];

    self.context = [NSManagedObjectContext context];
    [NSManagedObjectContext setDefaultContext:self.context];

    self.fetchController = [Message fetchRequestAllGroupedBy:nil withPredicate:nil sortedBy:@"text" ascending:YES];
    [self.fetchController setDelegate:self];
    [self.fetchController performFetch:nil];
}

Everytime i call [message save],the console logout: -NSManagedO开发者_如何学GobjectContext(MagicalRecord) mergeChangesFromNotification: Merging changes to * DEFAULT context on Main Thread *

But the NSFetchedResultsControllerDelegate never get called! Does this mean that I set the FetchedResultsController wrong or what? I am totally confused.

Thanks in advance.


The reason this isn't working is because MagicalRecord will automatically call performFetch: for you, thus not allowing you to set the delegate ahead of time.

Also, in your applicationDidFinishLaunching: method, you want to delete these lines:

self.context = [NSManagedObjectContext context];
[NSManagedObjectContext setDefaultContext:self.context];

You want to NOT change the default context in this case. MagicalRecord is handling stuff for you when you call setupCoreDataStackWithStoreNamed:...that is, a MOC is already available for use when that method completes, there is no need to toss the one it created for you and set the default context to a new instance in this particular case.

It's also not necessary to hold on to the context if all you're doing is going to use it to pass to one of the fetch methods provided by MagicalRecord. MagicalRecord will create a single context for its use (the 'default' context) and just use that...


I misunderstood what the [NSManagedObjectContext context] means. It create a new context in the main thread. Since the context that the fetchResultController monitor is not the same context that the change merged to, the NSFetchedResultsControllerDelegate will not get called.

Change [NSManagedObjectContext context] to [NSManagedObjectContext defaultContext] solved the problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消