ExBen
ExBen

Reputation: 185

MSSyncTable insert

I have a dictionary dict, the info is

(lldb) po dict
{
createdAt = "2016-05-25 06:29:16 +0000";
deleted = 0;
diskId = "f54b4bd9-694b-4856-8b8f-7f3afa18fd2e";
updatedAt = "2016-05-25 06:29:16 +0000";
userId = "4f119c15-2370-4ad7-bcbc-2cd4254b356e";
version = "1.0";
}

I call MSSyncTable insert function:

-(void)insert:(nonnull NSDictionary *)item completion:(nullable MSSyncItemBlock)completion;

the info is saved to local db successfully, but on the mobile service side, the received object don't have the info about version and updatedAt and createdAt, they are null, then mobile service return error to iOS app as below:

"{\"message\":\"The operation failed with the following error: 'A null store-generated value was returned for a non-nullable member 'Version' of type 'SaveOnPayService.Models.SOPUserStoresFavorite'.'.\"}"), NSLocalizedDescription=Not all operations completed successfully}

Is there anyone know what's going on here?

Update: My server side code is like

public async Task<IHttpActionResult> PostTodoItem(TodoItem item)
    {
        TodoItem current = await InsertAsync(item);
        return CreatedAtRoute("Tables", new { id = current.Id }, current);
    }

Do I need set createdAt, updateAt and version here?

Updated: the console output in Mobile Service side:

w3wp.exe Information: 0 : Message='ExBenStoresFavorite', Operation=DefaultHttpControllerSelector.SelectController w3wp.exe Information: 0 : Message='ExBenService.Controllers.ExBenStoresFavoriteController', Operation=DefaultHttpControllerActivator.Create w3wp.exe Information: 0 : Message='ExBenService.Controllers.ExBenStoresFavoriteController', Operation=HttpControllerDescriptor.CreateController w3wp.exe Information: 0 : Message='Selected action 'PostExBenStoresFavorite(ExBenStoresFavorite item)'', Operation=ApiControllerActionSelector.SelectAction w3wp.exe Information: 0 : Message='Value read='ExBenService.Models.ExBenStoresFavorite'', Operation=JsonMediaTypeFormatter.ReadFromStreamAsync w3wp.exe Information: 0 : Message='Parameter 'item' bound to the value 'ExBenService.Models.ExBenStoresFavorite'', Operation=FormatterParameterBinding.ExecuteBindingAsync w3wp.exe Information: 0 : Message='Model state is valid. Values: item=ExBenService.Models.ExBenStoresFavorite', Operation=HttpActionBinding.ExecuteBindingAsync w3wp.exe Information: 0 : Operation=TableQueryFilter.OnActionExecutingAsync w3wp.exe Information: 0 : Operation=TableControllerConfigAttribute.OnActionExecutingAsync w3wp.exe Information: 0 : Message='Will use same 'JsonMediaTypeFormatter' formatter', Operation=JsonMediaTypeFormatter.GetPerRequestFormatterInstance w3wp.exe Information: 0 : Message='Selected formatter='JsonMediaTypeFormatter', content-type='application/json; charset=utf-8'', Operation=DefaultContentNegotiator.Negotiate w3wp.exe Error: 0 : Message='The operation failed with the following error: 'A null store-generated value was returned for a non-nullable member 'Version' of type 'ExBenService.Models.ExBenStoresFavorite'.'.' w3wp.exe Warning: 0 : Message='UserMessage='The operation failed with the following error: 'A null store-generated value was returned for a non-nullable member 'Version' of type 'ExBenService.Models.ExBenStoresFavorite'.'.'', Status=400 (BadRequest), Exception=System.Web.Http.HttpResponseException: Processing of the HTTP request resulted in an exception. Please see the HTTP response returned by the 'Response' property of this exception for details. at Microsoft.Azure.Mobile.Server.Tables.EntityUtils.d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Azure.Mobile.Server.EntityDomainManager1.<InsertAsync>d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Azure.Mobile.Server.TableController1.d__3.MoveNext() Exception thrown: 'System.Web.Http.HttpResponseException' in mscorlib.dll

Upvotes: 0

Views: 257

Answers (3)

ExBen
ExBen

Reputation: 185

Finally I find the root cause. So I have a desktop which is also using EntityFramework and I use it to add data into my database. And the database is created by this Desktop App, So Version field in the database is Nullable, but if let mobile service create the database, then the Version field will be NOT NULL. that's the difference. so the fix is delete the database, can recreate through mobile service, it works now.

Upvotes: 0

Adrian Hall
Adrian Hall

Reputation: 8035

In a Mobile Service, the submitted service fields must have a double-underscore - i.e. __deleted, __createdAt, __updatedAt and __version. These must have specific formats - specifically, __deleted must be a boolean and __version must be a byte-sequence. In addition, you need an id field that is a GUID.

Upvotes: 0

lindydonna
lindydonna

Reputation: 3875

If you are trying to map the fields updatedAt, createdAt and version to the system properties, then you need to use the names ms_createdAt, ms_updatedAt and ms_version. Note that these cannot be set on the client, as they are set on the server side and returned to the client after the Push operation. To learn more about the Core Data model you need to use, see Get Started with Offline Data Sync in Mobile Services.

Also, to help in debugging, I recommend that you log your outgoing requests from your iOS client. See Log outgoing requests in iOS native client. You should log both the request and the response, and see if the system properties are actually being returned from your server.

Upvotes: 1

Related Questions