2012-03-22 126 views
1

我正在研究通過json/rest api從服務器檢索sqlite數據庫的iPhone應用程序。用戶可以將行添加到本地表中,並可以在本地進行更新。現在,當我在本地數據庫的表中添加了一些行時,我想從本地更新的數據庫中僅將這些新行同步/插入服務器數據庫。 如果有人知道該API方法(json/rest),請幫忙,或者如果有任何相關的教程,請幫助。如何通過json api更新服務器數據庫上的數據?

回答

3

當你說你正在檢索「sqlite」數據庫時,你的意思是所有表及其行的「json」表示嗎?我假設你實際上並沒有發送「sqlite」數據庫文件。

對於通過http發送和檢索json,爲簡單起見,可以使用NSURLConnection和NSURLRequest,因爲它們是內置的。如果要強制執行映射到核心數據,可以將RestKit框架用於連接和數據處理。

以下是前一種解決方案的示例實現 - 它假定您是ARC,否則您需要添加適當的保留和發佈語句。

1)聲明您使用的作爲適當的委託

@interface ClassName : NSObject <NSURLConnectionDelegate> 

2)聲明將用來接收數據

//interface 
    @property (nonatomic, strong) NSMutableData *responseData;  

    //implementation 
    @synthesize responseData; 

3 responseData對象)創建函數的類發送json請求

- (void)sendRequest 
{ 
    responseData = [NSMutableData data]; 

    //whatever your server address is 
    NSURL *url = [NSURL URLWithString:@"http://www.resturl.com/whatever"]; 

    //just sample data - create this dictionary with what you want to send 
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init]; 
    [params setObject:@"SomeValue" forKey:@"SomeKey"]; 


    NSError *jsonError; 
    //NSJSONSerialization is Apple's new json serialization class so we can use it to convert to and from json and foundation objects 
    NSData *requestdata = [NSJSONSerialization dataWithJSONObject:params options:0 error:&jsonError]; 

    NSMutableURLRequest *request; 
    request = [NSMutableURLRequest requestWithURL:url]; 
    [request setHTTPMethod:@"POST"]; 
    [request setValue:[NSString stringWithFormat:@"%d", [requestdata length]] forHTTPHeaderField:@"Content-Length"]; 
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
    [request setHTTPBody:requestdata]; 

    //this kicks off the request asynchronously 
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 

    //if you'd rather send a synchronous request, you can use the static NSURLConnection function 
    //sendSynchronousRequest:returningResponse:error: 
} 

4)實現委託功能接收我們的數據

//any time a piece of data is received we will append it to the responseData object 
    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
    { 
     [self.responseData appendData:data]; 
    } 

    //some sort of error, you can print the error or put in some other handling here, possibly even try again but you will risk an infinite loop then unless you impose some sort of limit 
    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
    { 
     // Clear the activeDownload property to allow later attempts 
     self.responseData = nil; 

    } 

    //connection has finished, thse requestData object should contain the entirety of the response at this point 
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection 
    { 
     NSError *jsonError; 
     NSDictionary *responseDict = 
     [NSJSONSerialization JSONObjectWithData:responseData 
             options:NSJSONWritingPrettyPrinted 
              error:&jsonError]; 
     if(responseDict) 
     { 
      NSLog(@"%@", responseDict); 
     } 
     else 
     { 
      NSLog(@"%@", [jsonError description]); 
     } 

     //clear out our response buffer for future requests 
     self.responseData = nil; 
    } 

如果要更新一些新的信息的遠程數據庫,只需跟蹤新行的地方(而不是僅僅與完整數據集將它們合併),併發送包含只有那些行的新要求到將添加它們的端點。這是在不執行實際映射的情況下執行此操作的最簡單方法。

相關問題