开发者

SQLite records not saved in the order they are created with Core Data?

While inspecting the sqlite database resulting from the code below I discovered that Core Data does not seem to save the objects (records) in the order that they are created.

-(void) CreateData
{
// Create an organization

NSManagedObjectContext *context = [self managedObjectContext];
NSEntityDescription *org_entity = [NSEntityDescription entityForName:@"Organization" inManagedObjectContext:cont开发者_开发知识库ext];
NSManagedObject *organization = [NSEntityDescription insertNewObjectForEntityForName:[org_entity name] inManagedObjectContext:context];

[organization setValue:@"MyCompany" forKey:@"name"];
[organization setValue:[NSNumber numberWithInt:[organization hash]] forKey:@"id"];


// Create several people

NSEntityDescription *person_entity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:context];
NSManagedObject *john = [NSEntityDescription insertNewObjectForEntityForName:[person_entity name] inManagedObjectContext:context];

[john setValue:[NSNumber numberWithInt:[john hash]]  forKey:@"id"];
[john setValue:@"John" forKey:@"name"];


NSManagedObject *jane = [NSEntityDescription insertNewObjectForEntityForName:[person_entity name] inManagedObjectContext:context];

[jane setValue:[NSNumber numberWithInt:[jane hash]]  forKey:@"id"];
[jane setValue:@"Jane" forKey:@"name"];


NSManagedObject *bill = [NSEntityDescription insertNewObjectForEntityForName:[person_entity name] inManagedObjectContext:context];

[bill setValue:[NSNumber numberWithInt:[bill hash]]  forKey:@"id"];
[bill setValue:@"Bill" forKey:@"name"];


// Designate the leader for "My Company"

[organization setValue:john forKey:@"leader"];


// Designate subordinates

NSMutableSet *johns_employees = [john mutableSetValueForKey:@"employee"];
[johns_employees addObject:jane];
[johns_employees addObject:bill];


NSError *error = nil;
if(![context save:&error])
{
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}
}

While I don't intend to rely on the order of the records for the real application it was interesting to learn that the order seems to be random. One would think that the underlying serialization routine would produce the same results every time.

Out of curiosity, why does this happen?


This is an interesting sentence :

'One would think that the underlying serialization routine would produce the same results every time'

CoreData abstracts away the underlying storage - it can be plist, xml, sqlite etc. You can't assume anything about the underlying routines, you don't know them :) They are determined by the type of storage that whoever created the NSPersistentStore (that your NSManagedObjectContext is connected to) asked for.

If you want to get the results back in a specific order, you need to give an NSOrderDescription to your NSFetchRequest ;)

PS @bryanmac's comment is very useful in knowing why sqlite might not return results in the right order.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜