定時器聽起來像實際啓動你的線程正確的方法。要設置了只是把這個在您的應用程序代理
[NSSTimer scheduledTimerWithTimeInterval:60.0 * 10.0 target:self selector:@selector(startBackgroundMethod) userInfo:nil repeats:YES];
然後創建你這樣的背景方法代碼:
- (void)startBackgroundMethod
{
//the timer calls this method runs on the main thread, so don't do any
//significant work here. the call below kicks off the actual background thread
[self performSelectorInBackground:@selector(backgroundMethod) withObject:nil];
}
- (void)backgroundMethod
{
@autoreleasepool
{
//this runs in a background thread, be careful not to do any UI updates
//or interact with any methods that run on the main thread
//without wrapping them with performSelectorOnMainThread:
}
}
至於是否實際上必要做這項工作在後臺線程,即取決於它是什麼。除非嚴格要求,否則應該避免使用線程,因爲可能存在併發錯誤,所以如果您告訴我們線程要做什麼,我們可以建議基於runloop的方法是否更合適。
嗨,尼克,謝謝你的回答。基本上線程擁有共享上下文並從持久存儲中檢索對象。然後它從對象獲取信息並將其上傳到Web服務。它不會以任何方式改變對象。這是因爲我正在考慮這種方法的時間安排和上傳。我正在使用ASIHTTPRequest上傳到web服務。 – Bjarke
您可能想重新考慮使用ASI,因爲開發人員已經正式放棄了它,並且它不適用於ARC。您可以使用常規的異步NSURLConnection完成上載,並且可以在runloop上運行而不會阻塞UI,也不需要從新線程中產生(我假設它在內部使用線程,但您不需要自己管理併發)。 –