Primary Key for created objects

Jul 26, 2010 at 12:22 PM
When I create a new object via the the proxy class I need to be able to get Primary Key of the newly created object on the server. I can see it gets returned in the response when I call saveChanges but there doesn't seem to be a way to access this via the odata lib. I'm aware of the delegate methods but I can only access the raw httpresponse object which is not ideal. Any suggestions?
Dec 13, 2010 at 8:46 PM

We've just started a new project using the objective-c client and we're running into the same issue as col_harris. I.e. we're not getting the primary key or the object that is created when calling saveChanges. Have you guys found an workaround?

Dec 17, 2010 at 11:41 PM

You can use the Objective-C NSURL library to retrieve what you need from the http response. The class HttpResponse has a Body property which would contain the XML you want. You can then use the Objective-C xml library to get the data you need.

Jan 20, 2011 at 5:52 PM

Here is sample code to retrieve a primary key after data insertion:

        DemoService *proxy=[[DemoService alloc]initWithUri:@"" credential:nil];
        ODataDemo_Product *p = [[ODataDemo_Product alloc] init];
        NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
        [fmt setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSS'Z'"];
        NSDate *d = [fmt dateFromString:@"2011-01-22T00:00:20.20"];
        [p setID:[NSNumber numberWithInt:5003]];
        [p setName:@"product_january_5003"];
        [p setDescription:@"product information:This is demo product information:5002"];
        [p setReleaseDate:[NSDate date]];
        [p setRating:[NSNumber numberWithInt:5]];
        [p setDiscontinuedDate:d];
        //[p setPrice:[NSDecimalNumber numberWithInt:1200]];
        [proxy addToProducts:p];
        [proxy saveChanges];
        NSArray *arr = [proxy.m_objectToResource values] ;
        if([arr count]>0){
                ResourceBox *box =  [arr objectAtIndex:0];
                ODataDemo_Product *p1 = box.m_resource;
                NSLog(@"=== product Information  ===");
                NSLog(@"product id...%d",[p1 getID]);
                NSLog(@"product name...%@",[p1  getName]);
                NSLog(@"product desc...%@",[p1  getDescription]);
                NSLog(@"product releaseDate...%@",[p1 getReleaseDate]);
                NSLog(@"product discontinuedDate...%@",[p1 getDiscontinuedDate]);
                NSLog(@"product rating...%d",[p1 getRating]);
                //NSLog(@"product id...%d",[p1 getPrice]);

        [p release];
        [proxy release];