NSOperationQueue and NSFetchedResultsController

女生的网名这么多〃 提交于 2020-01-04 11:05:29

问题


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

in my uitableviewcontroller 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


回答1:


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.



来源:https://stackoverflow.com/questions/6168361/nsoperationqueue-and-nsfetchedresultscontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!