NSOperationQueue和NSFetchedResultsController [英] NSOperationQueue and NSFetchedResultsController

查看:123
本文介绍了NSOperationQueue和NSFetchedResultsController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用queue和resultscontroller的组合来更新和显示一些coredata对象。



在我的uitableviewcontroller i每隔X秒调用我的主控制器对象中的一个方法。

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

}

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

[[MainController instance] updatePersons];

}



在此方法中,自定义NSOperation对象将添加到我的主要Q.

   - (BOOL)updatePersons {

UpdatePersonsOperation * u = [[UpdatePersonsOperation alloc]在里面];
[u setQueuePriority:NSOperationQueuePriorityVeryHigh];

[operationQ u];

[u release];

操作本身创建一个新的managedobjectcontext,并尝试从web下载一些xml并尝试更新



在我的主控制器中,我收到上下文改变消息,并使用mergeChangesFromContextDidSaveNotification合并和更新我的主对象上下文。所有resultscontroller使用这个主对象上下文。

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

实际上一切正常,但是当我在NSOperation中插入一个新对象时,它需要4-6秒,直到UI更新并显示这个新对象...此外,UI块...它不可能滚动或触发其他交互...



当我不使用队列,我把我的代码下载和更新对象到我的uitableviewcontroller类中的一个方法,我使用这个

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

一切正常,没有任何延迟或UI冻结...


div>

另一个问题可能是更新UI需要在主线程上进行。我遇到了同样的问题,你报告,并最终搞清楚,如果你调用

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

这将导致UI在线程完成处理后立即更新。否则,它会在UI动画发生前等待大约5秒。


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

解决方案

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.

这篇关于NSOperationQueue和NSFetchedResultsController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆