2014-01-08 36 views
1

执行用于从手机访问地址簿并保存到本地sqlite3数据库的后台进程。我做了如下。如何运行后台进程/多线程进程,以获得更好的应用程序性能目标c

[self performSelectorInBackground:@selector(syncAddressBookToDB:) withObject:nil]; 
- (void)syncAddressBookToDB:(id)sender 
{ 
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 

// accessing the address book and saving to the database. 
    GVDBManager *objDB = [GVDBManager getSharedInstance]; 
    [objDB getContactsFromAddBook]; 
    [objDB syncPhoneBookWithVcardsTable]; 

    [pool drain]; 
} 

有没有更好的方法来实现背景/多线程过程来改善我的应用程序的性能。任何人请为我提出更好的解决方案。

+0

为什么?它不够好吗?你已经在另一个线程上工作,如果它运行缓慢,那么你的问题是在运行代码,而不是线程',我希望我很清楚.. –

+3

如果你想多一点控制线程,但不要太复杂,请在此处查看GCD:https://developer.apple.com/library/mac/documentation/performance/reference/gcd_libdispatch_ref/Reference/reference.html – Jack

回答

1

使用Grand Central Dispatch通常是更好和更推荐的方式来处理这样的任务,使用performSelectorInBackground:方法。你可以这样做:

dispatch_queue_t loadQueue = dispatch_queue_create("Image loader", NULL); 
dispatch_async(loadQueue, ^{ 
    // Your code to run in the background here 
}); 
相关问题