CharleyXIV
CharleyXIV

Reputation: 1610

Azure Mobile Service Offline Data Sync - The item provided was not valid

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

Upvotes: 1

Views: 525

Answers (2)

Shri
Shri

Reputation: 46

@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.

Upvotes: 3

phillipv
phillipv

Reputation: 1717

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.

Upvotes: 2

Related Questions