RestKit .20 with CoreData - How to set default values - restkit

I am using RestKit .20 with CoreData. I have RestKit calling the JSON API successfully and storing the response to the CoreData model without any issues.
My question is how do I set default values at mapping time that are not on the response?
In my situation, I am downloading a list of Alerts to an alert inbox. I need to show which alerts have been read on the local device. I have a CoreData attribute on the entity model called AlertRead (boolean)which I update when the user marks the Alert as 1=read.
My question is how do I set the default value of the attribute to 0=unread at the time the data is retrieved and mapped.
I tried to set the default value in the xcdatamodeld file and this did not work. It appears RestKit sets the value to Nil at mapping time.
One point of clarification..I do not think I want to set this attribute by mapping the attribute to a value on the api because the refresh of the data would override the current data on the local database.
Current code for mapping.
RKEntityMapping* alertMapping = [RKEntityMapping mappingForEntityForName:#"AlertMessage" inManagedObjectStore:_managedObjectStore];
[alertMapping addAttributeMappingsFromDictionary:#{
#"alertSubject": #"subject",
#"alertDetailMessage": #"detailMessage",
#"id": #"alertId",
}];
Thanks for any suggestions.
G

Look at using KVC validation (which RestKit utilises heavily) to verify the incoming data from RestKit and rejecting the update if it isn't appropriate. This should be used in conjunction with default values set in the xcdatamodel.

Related

Parse cloud code send flag to save object

I've been trying to send a flag to save request. This save request is sent from different platforms so we seperated them with a flag. The problem is that it gives
Result: TypeError: Cannot read property '0' of undefined
when you send the request to table without the parameter. I did not want to add this parameter as a column in the table but it seems it automatically creates it when you successfully save the object. Is there a way to save the object without creating the flag column and seperate the save requests with and without the flag? Thank you in advance.
Parse.Cloud.beforeSave("MessageTest", function(request, response) {
if(!request.object.get("fromMessages")) {
..
..
}
else response.succcess();
});
If you try to save an object directly from your app, you cannot remove the field from your request in beforeSave trigger. A better approach is to save your object via a Cloud function instead. Send in your object alongside of your platform flag to a cloud function, then construct a MessageTest object from the parameters (obviously ignoring your platform flag) and then save it from there.

Parse, Add option to User in Data Browser

I wanted to create a new option in the Data Browser (just like the "username", "password", "authData"...fields), to hold a monetary value for my game. So a new field called "money" that will hold a value.
However, I'm not exactly sure how to go about doing so.
I took a look at the documentation and found things like .add but I wasn't sure exactly what was going on in the code.
Does anyone know a way to do this?
In the data browser, there is a "+ Col" button which allows you to add another field to your class. You can then set the name of the field and it's type.
You can also do this from their SDK's by simply setting the field, even if you have not added it via the data browser.
Here is what the Parse docs say regarding this:
Storing data through the Parse REST API is built around a JSON encoding of the object's data. This data is schemaless, which means that you don't need to specify ahead of time what keys exist on each object. You simply set whatever key-value pairs you want, and the backend will store it.
Source: https://parse.com/docs/rest#objects

About the object sequence in the RestKit

I recently used RestKit to handle my net request affairs. There is a solution for the sort with the sort descriptor. But there is no sort key for the data sent by the server.
How can I keep the data in the same sequence as the server.
There is a solution that I can add a sortID in the object, but this is not very elegant. I want to know if there is any api in RestKit for this problem?
You should add sortID to the object - this is the appropriate solution. To populate it with a value you need to use the #metadata made available to your mappings:
#"#metadata.mapping.collectionIndex" : #"sortID"
This code assumes that you are specifying your mapping with a dictionary (addAttributeMappingsFromDictionary:).
Documented here, the collectionIndex provides you with an NSNumber representing the order of the item in the response data.

Kendo UI Datasource and Arrays

I am sending over a series of array values from a posted form to an MVC3 Controller. I was hoping the default modelbinder would be able to parse this but I'm having some difficulty with it.
The array is in the following format:
order[0].[type]=some value.
I think this is the reason the model binder is not parsing my values because I'm not getting anything populated in my model.
What would be another way to handle this?
Probably need to post more of your code so I can see what you are doing exactly. However saying this you need to pass the model to the view/partial view on the response you are trying to retrieve on the post request.
If not you will have to iterate through the Form Collection that will be returned and the Actions Methods type e.g. ActionMethodName(FormCollection form), one issue is name versus id its the name of the Kendo UI control that is used to get the value not the id.
1As far as I remember the right format was:
orders[0].OrderID=13;
orders[0].Name="test";
orders[1].OrderID=15;
orders[1].Name="again test";
The indexing should start from 0 and increase by 1.
Check this out: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

Hold a data object temporarily

I have a object that i want to store for a moment. The object is in a controller for now, the controller will generate a view. A AJAX request is made from the view to next controller. For that moment i need the object previously stored. Previously, i used session and it worked well. But not sure it is the right thing to do. Is session the answer for this or is there anything else?
P.S. I need to access the data multiple time.
You can put your object to cache.
Add:
HttpContext.Current.Cache.Add("MyData", myObject, null, DateTime.Now.AddHours(6),
System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Normal, null);
Retrive object:
var my object = HttpContext.Current.Cache["MyData"] as MyType

Resources