我執行以下方法:在這種情況下,主隊列/當前隊列和主線程/後臺線程之間的區別?
MotionHandler.m
-(void)startAccelerationUpdates
{
[motionManagerstartDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue]withHandler:^(CMDeviceMotion *motion, NSError *error){.....}
}
在後臺線程,如下所示:
[currentMotionHandler performSelectorInBackground:@selector(startAccelerationUpdates) withObject:nil];
但是上述方法使用main Queue
(其是在主線程)來執行必要的更新,即使我在後臺線程調用它..所以加速更新正在執行後臺線程或ma在線程中,我很困惑..?
更有趣的是,當我在後臺線程上再次調用上述方法時,這次使用current Queue
,我沒有更新。有人可以解釋一下運行之間的區別:
1. a background thread but on the main queue
2. a background thread but on the current queue
3. the main thread but on the main queue
4. the main thread but on the current queue
在當前的實現?謝謝!
謝謝大家既爲你的答案,很受啓發哉!但是,我經歷了NSOperation&NSOperationQueue類的引用,並且沒有關於如何執行該操作的指導原則,即如何在後臺線程上創建隊列並在該隊列上運行(例如加速更新)。這是明顯需要做的事情,因爲在後臺運行加速更新是基於文檔的最安全的方法 –
如果您創建自己的隊列= NSOperationQueue * foo = [NSOperationQueue new] ;,則將操作排隊,將在後臺線程上運行而不是mainThread。閱讀Apple Concurrency Guide。您可能會發現,塊現在是更好的選擇。在使用NSURLConnections時,我只使用NSOperations來實現併發。你可以看到在Github上一個簡單的項目中,我寫道:https://github.com/dhoerl/NSOperation-WebFetches-MadeEasy - 你會看到如何創建一個隊列,併發操作等。它的一個小項目一些文件。 –
謝謝大衛我會看看你的項目,看看如何正確實施你的建議。 –