0

Following this [post][1], I am trying to parse out the numerical portion of the ObjectID in a ManagedObject where an auto increment apparently resides that is not ordinarily exposed through core data api. However, I always get 0 as a response.

Has anyone else done this or can anyone see why this is not working? Thanks in advance for any suggestions.

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Item" inManagedObjectContext:self.managedObjectContext];

    Items *newItem = [[Items alloc]initWithEntity:entity insertIntoManagedObjectContext:self.managedObjectContext];
    NSURL *url = [[newItem objectID] URIRepresentation];
    //get full url
    NSString *url_string = [url absoluteString];
        //split with a /
    NSArray *parts = [url_string componentsSeparatedByString:@"/"];

    NSString *last_segment=[parts objectAtIndex:[parts count]-1];

    //p#
    NSString *number_part = [last_segment stringByReplacingOccurrencesOfString:@"p" withString:@""];

    NSUInteger auto_id = [number_part integerValue];

    number_part =nil;
    last_segment=nil;
    parts=nil;
    url=nil;
    url_string=nil;
    NSUInteger final_id = auto_id;
    NSLog(@"NOW DISPLAYING AUTOID: autoid is:%lu",(unsigned long)final_id);
user1904273
  • 4,562
  • 11
  • 45
  • 96
  • 1
    This is an incredibly bad idea. You would be relying on undocumented behavior that could change at any time. Even if you make it work, it's a terrible, horrible idea. – Tom Harrington May 04 '15 at 16:39
  • The idea that there was an auto increment behind the scenes that is not exposed by Core Data that could be somehow used was appealing. However, from the ObjectID that is showing up in the log on my simulator, it looks like my ObjectID does not follow the same naming convention..so yes I am giving up on this idea. – user1904273 May 05 '15 at 21:16
  • Looks like I will have to use a universal ID. However, to be fair, as has been said on many other SO questions, synchronizing between app and server is non trivial so worth exploring different possibilities. I still have to figure out how to sync without giving records on the server universal ids. Looking at Chris method here http://stackoverflow.com/questions/5035132/how-to-sync-iphone-core-data-with-web-server-and-then-push-to-other-devices but perhaps using server to generate the archival auto incremented ids and using universal Ids on app client temporarily. – user1904273 May 05 '15 at 21:25
  • 1
    And by the way what do you really think about it? – user1904273 May 05 '15 at 23:50

1 Answers1

0

As Tom Harrington mentioned, this is an absolutely terrible idea. This is undocumented, can and will change and you have no guarantee that it is going to continue to increment between lifecycles of the application.

Create your own unique identifier, do not rely on the -objectID.

Marcus S. Zarra
  • 46,571
  • 9
  • 101
  • 182
  • Yes I have come around to your and Tom's point of view. I guess for the app I will have to use a GUID at least prior to syncing but still hoping to preserve the auto incremented scheme used on server. – user1904273 May 05 '15 at 21:30