2015-08-21 25 views
1

我正在使用Azure移動服務作爲iOS應用程序的後端。我已經設置了所有與脫機同步一起工作的功能,即使沒有網絡連接,也可以查看,添加或修改數據。我現在正在進行測試,並遇到一個錯誤:當我嘗試同步數據時,「提供的項目無效」。Azure移動服務離線數據同步 - 提供的項目無效

下面是我在做什麼:

我添加了一個新的運動員到syncTableWithName:@ 「運動員」 與此:

NSDictionary *newItem = @{@"firstname": @"Charles", @"lastname": @"Lambert", @"laterality" : @"Orthodox"}; 

     [self.athletesService addItem:newItem completion:^{ 
      NSLog(@"New athlete added"); 
     }]; 

這裏的的addItem功能:

-(void)addItem:(NSDictionary *)item completion:(CompletionBlock)completion 
{ 
    // Insert the item into the Athlete table 
    [self.syncTable insert:item completion:^(NSDictionary *result, NSError *error) 
    { 
     [self logErrorIfNotNil:error]; 

     // Let the caller know that we finished 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      completion(); 
     }); 
    }]; 
} 

對於現在一切正常,並且該項目在syncTable中。問題是當我嘗試與Azure移動服務同步時。這裏的syncData功能我打電話:

-(void)syncData:(CompletionBlock)completion 
{ 
    // push all changes in the sync context, then pull new data 
    [self.client.syncContext pushWithCompletion:^(NSError *error) { 
     [self logErrorIfNotNil:error]; 
     [self pullData:completion]; 
    }]; 
} 

的pushWithCompletion得到我的錯誤:「提供的項目是無效」和相同的pullData函數被調用後:

-(void)pullData:(CompletionBlock)completion 
{ 
    MSQuery *query = [self.syncTable query]; 

    // Pulls data from the remote server into the local table. 
    // We're pulling all items and filtering in the view 
    // query ID is used for incremental sync 
    [self.syncTable pullWithQuery:query queryId:@"allAthletes" completion:^(NSError *error) { 
     [self logErrorIfNotNil:error]; 

     // Let the caller know that we finished 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      completion(); 
     }); 

    }]; 
} 

我試着直接插入MSTable中,並且工作正常。實際上,當我使用MSSyncTable時,我遇到了這個錯誤。雖然當我手動在我的數據庫中插入數據,並且我同步我的上下文時,我可以獲取數據並在我的UITableView中顯示。

Here is my Athlete table 期待着看看你們對此的看法。非常感謝!

我剛剛編輯了我的問題,感謝@phillipv。 當我像使用NSDictionary一樣添加一個項目時,我遇到了錯誤「提供的項目無效」。所以,我想通過先插入到我的managedObjectContext,然後調用添加項:

NSDictionary *dict = [MSCoreDataStore tableItemFromManagedObject:newAthlete]; 

我然後我得到的錯誤,當我嘗試同步:「所提供的項目沒有一個有效的ID」

我覺得我遇到了一圈..:S

+0

這很有趣,錯誤意味着你的本地表中出現的版本不能正確解析爲JSON。 CoreData中定義的運動員表格如何?你可以添加一個MSSyncContextDelegate並實現tableOperation:OnComplete,並記錄operation.item以查看是否有任何關於它的obv不正確? – phillipv

+0

我剛剛添加了我的運動員表格說明@phillipv – CharleyXIV

+0

以下是@phillipv提供的operation.item的輸出:操作項目:{aliasname =「」; birthdate =「」; firstname = N; height = 0; id =「AD76405C-6A3D-4FE7-98D9-C3299326AAC9」; lastname = B;偏側性=正統;國籍=「」;達到= 0;性別= 0; sparrings =「{(\ n)}」;重量= 0; } 很顯然,名字,姓氏和橫向性不會以字符串形式輸出。我只是不知道爲什麼。 – CharleyXIV

回答

3

@ Charley14,您可以通過添加以下處理程序來解決該問題。

- (void)tableOperation:(nonnull MSTableOperation *)operation onComplete:(nonnull MSSyncItemBlock)completion 
{ 
    NSMutableDictionary *rwItem = [NSMutableDictionary dictionaryWithDictionary:operation.item]; 

    // Temporary workaround 
    [rwItem removeObjectsForKeys:@[ @"relationship1", @"relationship2"]]; 

    operation.item = rwItem; 

    [operation executeWithCompletion:completion]; 
} 

tableOperation:onComplete:處理程序只是刪除對應於關係的鍵。您必須將代碼段中的'relationship1','relationship2'替換爲應用程序中實際關係的名稱。 修復了錯誤(https://github.com/Azure/azure-mobile-services/issues/779)後,可以刪除此解決方法。

+0

這確實解決了我的問題暫時。非常感謝你! – CharleyXIV

2

這似乎是在iOS SDK中的一個錯誤,因爲多對一的關係不應該在給予操作的對象被曝光推送電話期間。

創建以下錯誤更多的細節在GitHub上:https://github.com/Azure/azure-mobile-services/issues/779

錯誤信息的原因是由於這樣的事實,我們的關係在對象上的NSSet,和NSJSONSerializer拋出,因爲它不知道如何將其轉換爲JSON。

+0

這是否意味着我現在無法解決我的問題? – CharleyXIV

+0

你知道這個bug是否會很快得到糾正? @philipv – CharleyXIV

相關問題