2011-03-18 41 views

回答

1

一些想法:

  1. 可以運行在單獨的線程下載過程。

  2. 寫類,如下

@interface FileDownloader:的NSOperation

//使用以下方法:

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest: 
         [NSURLRequest requestWithURL: 
          [NSURL URLWithString:fileRecord.fileURLString]] delegate:self startImmediately:YES]; 
3

NSURLConnection的及其委託方法將允許一個URL請求的異步(後臺線程)加載。

請參考NSURLConnection Class Reference

從你應該分析它的另一個輔助線程服務器獲取數據後。然後你可以將它保存到數據庫。

您可以在Apple示例應用程序中找到更好的演示。請檢查TopPaid app

此示例應用程序沒有數據庫管理模塊。但會教你開發一個通用(iPad和iPhone兼容的應用程序)。

1

您可以使用下面的方法線程使用脫離線程

[NSThread detachNewThreadSelector:@selector(yourMethod) toTarget:self withObject:nil]; 

現在在方法執行你的任務

-(void) yourMethod { 
//ur work 

} 

好運

+0

謝謝你,我確實使用這種方法來調用下載類(連接方法) – NIKHIL 2011-03-18 14:51:46

0

當從服務在後臺下載,我更喜歡使用在單獨線程上運行的同步調用。這是我在大多數應用程序中的做法。

打電話給我的那個旋轉單中的一個新的線程

[[MyServiceSingleton sharedInstance] doSomeWorkInBackground:param1]; 

通用的方法 - 定義私有方法 - doSomeWorkBackgroundJob(我用的是空類中的方法)背景中doSomeWorkInBackground方法

[self performSelectorInBackground:@selector(doSomeWorkBackgroundJob:) withObject:param1]; 

內調用工作 - 創建池,做工,排水池

- (void)doSomeWorkBackgroundJob:(NSString *)param1 { 
NSAutoreleasePool *pool; 
pool = [[NSAutoreleasePool alloc] init]; 
assert(pool != nil); 

// you can call another method here or just create your synchronous request and handle the response data 

[pool drain]; 

}

相關問題