Ros*_*ist 3 multithreading location ios ios5
我目前正在研究一个应用程序,它在位置发生变化时解析APPdelegate类中的一些JSON数据.
我的问题是:"最合适的方式是怎么做的?" 目前,在解析数据时,应用程序被"冻结",直到数据被加载为止.
我需要一些建议:)
谢谢
ayo*_*yoy 10
当然也有几个方面,包括NSThread,NSOperation和老式的libpthread.但我发现最方便的(特别是对于简单的后台任务)libdispatch也被称为Grand Central Dispatch.
使用调度队列,您可以快速将耗时的任务委派给单独的线程(或者更准确地说,执行队列--GCD决定它是线程还是异步任务).这是最简单的例子:
// create a dispatch queue, first argument is a C string (note no "@"), second is always NULL
dispatch_queue_t jsonParsingQueue = dispatch_queue_create("jsonParsingQueue", NULL);
// execute a task on that queue asynchronously
dispatch_async(jsonParsingQueue, ^{
[self doSomeJSONReadingAndParsing];
// once this is done, if you need to you can call
// some code on a main thread (delegates, notifications, UI updates...)
dispatch_async(dispatch_get_main_queue(), ^{
[self.viewController updateWithNewData];
});
});
// release the dispatch queue
dispatch_release(jsonParsingQueue);
Run Code Online (Sandbox Code Playgroud)
上面的代码将在单独的执行队列中读取JSON数据,而不是阻塞UI线程.这只是一个简单的例子,GCD还有很多,所以请查看文档以获取更多信息.
| 归档时间: |
|
| 查看次数: |
3457 次 |
| 最近记录: |