0

In my application I have to send values of my NSManagedObject to server & after getting a success result from server I must update a filed in my NSManagedObject.

On the server side the id field is an integer type. Is there any possible id in Core Data which i can send to sever and update the value again in my iPhone?

Is NSManagedObjectID suitable for this ?

2

3 Answers 3

7

NSManagedObjectID is really not appropriate for this. It's not numeric and doesn't convert to a numeric form. And if you're communicating on a server-- can the user use the same server account from more than one device? Because NSManagedObjectID is only valid on the device where the object was created. You can't create a managed object and tell it what object ID to use.

If you need a unique, numeric ID, you need to create one yourself. Add a numeric attribute to the entity description and use it for your own ID values. Core Data does not provide this, but it's easy to do it yourself.

1

NSManagedObjectID is fully under Core Data's control. That means you have little if no control over it. Say you're creating a database from fresh using data from the server, there is no way to instruct Core Data to reuse specific NSManagedObjectIDs (so you'll have to update the IDs on the server). NSManagedObjectID is meant to be used within Core Data in most cases.

You should use your own unique IDs, and this is pretty easily done with the following code:

NSString* uuid = [NSUUID new].UUIDString;
NSString* entityID = [[NSString alloc] initWithFormat: @"%@/%@", self.entity.name, uuid];

You can use either the uuid directly or use another ID scheme like /YourEntityName/<uuid> or whatever suits you.

0

NSManagedObjectID is unique enough for that purpose, no problem. But it changes its value exactly once - at leat it may. That is when a transient object becomes persistent. So make sure to save the context before you fetch the NSManagedObject's ID.

3
  • My server field is an integer unfortunately i can't change that.Is it possible to get a unique integer from NSManagedObjectID
    – Eldhose
    May 24, 2013 at 11:19
  • I'd say no. Can't you make your server deliver a unique ID? I mean, the ID must be unique in the server's data, right? May 24, 2013 at 14:58
  • The real situation is I have to send some data of object to server and on getting response back I should update it in coredata. My requirement is to uniquely identify the filed and update my local
    – Eldhose
    May 27, 2013 at 3:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.