开发者

NSOperationQueue and NSFetchedResultsController

开发者 https://www.devze.com 2023-03-09 05:56 出处:网络
i use a combination of queue and resultscontroller to update and display some coredata objects. in my uit开发者_开发技巧ableviewcontroller i call every X second a method in my main controller object.

i use a combination of queue and resultscontroller to update and display some coredata objects.

in my uit开发者_开发技巧ableviewcontroller i call every X second a method in my main controller object.

[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(test:)            userInfo:nil repeats:YES];

}

- (void)test:(NSTimer*)theTimer {

[[MainController instance] updatePersons];

}

In this method a custom NSOperation object will be added to my main Q.

- (BOOL)updatePersons {

UpdatePersonsOperation* u = [[UpdatePersonsOperation alloc] init];
[u setQueuePriority:NSOperationQueuePriorityVeryHigh];

[operationQ u];

[u release];

The operation itself creates a new managedobjectcontext and tries to download some xml from the web and tries to update the coredata database... (This code works!)

In my main controller i receive the context changed message and i use mergeChangesFromContextDidSaveNotification to merge and update my main object context. All resultscontroller use this main object context.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationManagedObjectContextDidSave:) name:
                                        NSManagedObjectContextDidSaveNotification object:nil];

Actually everything works but when i insert a new object inside a NSOperation it takes 4-6 seconds till the UI updates and displays this new object... Furthermore the UI blocks... Its not possible to scroll or trigger a other interaction...

When i don't use a queue and i put my code to download and update the objects into a method inside my uitableviewcontroller class and i use this

[NSThread detachNewThreadSelector:@selector(codeFromUpdatePersonsOperation) toTarget:self withObject:nil];

Everything works very well without any delay or UI freeze...

Can someone explain me this behavoir?

Thanks


Another problem may have been that updating the UI needs to take place on the Main thread. I was experiencing the same issue that you reported, and eventually figured out that if you call

[target performSelectorOnMainThread:@selector(methodToUpdateUI:) withObject:dataFromRetrievalOperation waitUntilDone:NO];

This will cause the UI to update immediately when the thread has finished processing. Otherwise, it waits about 5 seconds before the UI animations take place.

0

精彩评论

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