2012-03-22 17 views
1

私はjson/rest apiを通じてサーバーからsqliteデータベースを取得するiPhoneアプリケーションで作業しています。また、ユーザーはテーブルにローカルに行を追加し、ローカルに更新することができます。今、私はローカルデータベースのテーブルにいくつかの行を追加したので、私は同期する/私のローカル更新されたデータベースからサーバーデータベースにそれらの新しい行を挿入したい。 誰かがそのapiメソッド(json/rest)について知っていれば助けてください。それに関連するチュートリアルがあれば、助けてください。json apiからサーバーdbのデータを更新するには?

答えて

3

"sqlite"データベースを取得しているとしたら、すべてのテーブルとその行の "json"表現を意味しますか?私はあなたが実際に "sqlite" dbファイルを送信していないと仮定しています。

http経由でjsonを送信および取得する場合は、NSURLConnectionおよびNSURLRequestを組み込み済みなので簡単に使用できます。コアデータへのマッピングを実行する場合は、 。

これは、以前のソリューションの実装例です。これは、ARCであることを前提としています。それ以外の場合は、適切なretainステートメントとreleaseステートメントを追加する必要があります。

1)

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

    //implementation 
    @synthesize responseData; 

3データを受信するために使用されるresponseDataオブジェクトを宣言)

@interface ClassName : NSObject <NSURLConnectionDelegate> 

2適切な委譲として使用しているクラスを宣言)という関数を作成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; 
    } 

あなたは、いくつかの新しい情報を使用して、リモートデータベースを更新するだけで、ローカルに(だけではなく、完全なデータセットでそれらをマージ)新しい行を追跡し、行のみを含む新しいリクエストを送信したい場合は

それらを追加するエンドポイントに送信します。これは実際のマッピングを強制することなくこれを行う最も簡単な方法です。

関連する問題