在客觀C, 我讓我的程序中使用while循環在iOS編程中使用while(true)語句是一種有效的方法?
doInitialize()
{
dispach_group_t loadDataGroup=dispatch_group_create();
dispatch_group_async(loadDataGroup,...get_global_queue(..),0),^{
renewauth();
}
dispatch_group_notify(loadDataGroup,...get_global_queue(..),0),^{
//Do other tasks once renew session has completed...
}
}
renewauth()
{
RenewAuthTokenInProgress=true;
startRenewThread();
**while (RenewAuthTokenInProgress);**
}
反過來startRenewThread()函數裏面也進行dispatch_async操作等。所以我必須使renewAuth()等待。
一旦更新成功,startRenewThread中的異步任務將更新bool變量。
除了dispatch_groups之外,還有更好的方法嗎? 讓其他線程與while(true)語句等待是否好?
忙碌的等待是一個可怕的想法。爲什麼你要在後臺運行這個更新功能? – Thilo
如果你使用異步路由,dispatch_groups有什麼問題? – Thilo
假設RenewAuthTokenInProgress保持一段時間,然後是你的iPhone會變熱。您將以100%運行其中一個內核。它也會使你的電池變平。 – TJA