1

I am using Azure Mobile Service as a backend for an iOS app. I have set up everything to work with offline sync which allows me to view, add, or modify data even when there is no network connection. I am now into testing and I run into an error: "The item provided was not valid" when I try to synchronize data.

Here's what I am doing:

I add a new athlete to the syncTableWithName:@"Athlete" with this:

NSDictionary *newItem = @{@"firstname": @"Charles", @"lastname": @"Lambert", @"laterality" : @"Orthodox"}; [self.athletesService addItem:newItem completion:^{ NSLog(@"New athlete added"); }]; 

Here's the addItem function:

-(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(); }); }]; } 

For now everything is fine and the item is in the syncTable. The problem is when I try to synchronize with the Azure Mobile Service. Here's the syncData function I am calling:

-(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]; }]; } 

The pushWithCompletion gets me the error: "The item provided was not valid." and same for the pullData function that gets called after:

-(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(); }); }]; } 

I have tried inserting directly in the MSTable and that works fine. It's really when I am using the MSSyncTable that I run into this error. Although when I insert data manually in my database and that I synchronize my context I can fetch data and display within my UITableView.

Here is my Athlete table Lookin forward to see what you guys think about this. Thanks a lot!

I just edited my question thanks to @phillipv. When I add an item using NSDictionary just like I did I run into the error "The item provided was not valid". So I tried adding an item by first inserting it to my managedObjectContext and then calling:

NSDictionary *dict = [MSCoreDataStore tableItemFromManagedObject:newAthlete]; 

I then I get the error when I try to sync: "The item provided did not have a valid id."

I feel like I am experiencing a circle.. :S

3
  • That's interesting, the error implies the version coming out of your local table isn't parsing correctly into JSON. How is your athlete table defined in CoreData? Can you add a MSSyncContextDelegate and implement tableOperation:OnComplete, and log the operation.item to see if anything is obv incorrect about it? Commented Aug 21, 2015 at 20:45
  • I just added my athlete table description @phillipv Commented Aug 21, 2015 at 20:49
  • Here's the output of operation.item as ask by @phillipv : Operation item: { aliasname = "<null>"; birthdate = "<null>"; firstname = N; height = 0; id = "AD76405C-6A3D-4FE7-98D9-C3299326AAC9"; lastname = B; laterality = Orthodox; nationality = "<null>"; reach = 0; sex = 0; sparrings = "{(\n)}"; weight = 0; } Obviously the firstname, lastname and laterality are not output as strings. I just don't know why. Commented Aug 21, 2015 at 21:27

2 Answers 2

3

@Charley14, you can work around the bug by adding the following handler.

- (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]; } 

The tableOperation:onComplete: handler is simply removing keys that correspond to the relationships. You will have to replace 'relationship1', 'relationship2' in the code snippet with names of actual relationships in your application. Once the bug (https://github.com/Azure/azure-mobile-services/issues/779) is fixed, this workaround can be removed.

Sign up to request clarification or add additional context in comments.

1 Comment

This does solve my problem temporarily. Thank you very much!
2

This appears to be a bug in the iOS SDK, as the Many to One relationship is not supposed to be exposed in the object given to the operation during a Push call.

Created the following bug with more details on GitHub: https://github.com/Azure/azure-mobile-services/issues/779

The cause of the error message is due to the fact that the relationship is a NSSet on the object, and the NSJSONSerializer throws as it does not know how to convert that to JSON.

1 Comment

Do you have any idea if this bug will be corrected soon? @philipv

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.