我遇到有关CoreData和与服务器数据库同步的问题。当我在服务器上同步两个实体和两个表(RIGHTS和CUSTOMERS)时,问题是如果服务器上有外键,我该如何管理它?例如 :
// CUSTOMERS SYNCHRONIZATION
NSLog(@"Value : %@", [[jsonDict valueForKey:@"Customers"] objectAtIndex:0]);
for (id object in [[jsonDict valueForKey:@"Customers"] objectAtIndex:0])
{
NSLog(@"%@", object);
// if jsonDict.customers.id_customer pas présent dans table Customers
//Recherche id_customer dans CoreData
NSError *error = nil;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id_customer = %@", [object valueForKey:@"id_customer"]]; //create a filter
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Customers"]; //create a query
request.predicate = predicate; //set the filter on the query
NSArray *customerSearch = [[self managedObjectContext] executeFetchRequest:request error:&error]; //execute the query
if ([customerSearch count] == 0)
{
NSManagedObject *customers = [NSEntityDescription
insertNewObjectForEntityForName:@"Customers"
inManagedObjectContext:context];
[customers setValue:[NSNumber numberWithInt:[[object valueForKey:@"id_customer"] intValue]] forKey:@"id_customer"];
[customers setValue:[object valueForKey:@"address"] forKey:@"address"];
[customers setValue:[object valueForKey:@"firstname"] forKey:@"firstname"];
[customers setValue:[object valueForKey:@"lastname"] forKey:@"lastname"];
[customers setValue:[NSNumber numberWithInt:[[object valueForKey:@"id_right"] intValue]] forKey:@"id_right"];//Foreign Key problem
}
else if ([customerSearch count] == 1)
{
//Update the object
Customers *customers = [[context executeFetchRequest:request error:&error] lastObject];
customers.address = [object valueForKey:@"address"];
customers.firstname = [object valueForKey:@"firstname"];
customers.lastname = [object valueForKey:@"lastname"];
customers.id_right = [NSNumber numberWithInt:[[object valueForKey:@"id_right"] intValue]];//Foreign Key problem
}
else
{
NSLog(@"WARNING - May be problems with duplicate id_customer in database"); //if > 1 then problem
}
if (![context save:&error])
{
NSLog(@"Whoops, couldn't save Customers: %@", [error localizedDescription]);
}
}
// RIGHTS SYNCHRONIZATION
NSLog(@"Value : %@", [[jsonDict valueForKey:@"Rights"] objectAtIndex:0]);
for (id object in [[jsonDict valueForKey:@"Rights"] objectAtIndex:0])
{
NSLog(@"%@", object);
// if jsonDict.customers.id_customer pas présent dans table Customers
//Recherche id_customer dans CoreData
NSError *error = nil;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"id_right = %@", [object valueForKey:@"id_right"]]; //create a filter
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Rights"]; //create a query
request.predicate = predicate; //set the filter on the query
NSArray *customerSearch = [[self managedObjectContext] executeFetchRequest:request error:&error]; //execute the query
if ([customerSearch count] == 0)
{
NSManagedObject *rights = [NSEntityDescription
insertNewObjectForEntityForName:@"Rights"
inManagedObjectContext:context];
[rights setValue:[NSNumber numberWithInt:[[object valueForKey:@"id_right"] intValue]] forKey:@"id_right"];
[rights setValue:[object valueForKey:@"name"] forKey:@"name"];
}
else if ([customerSearch count] == 1)
{
//Update the object
Rights *rights = [[context executeFetchRequest:request error:&error] lastObject];
rights.name = [object valueForKey:@"name"];
}
else
{
NSLog(@"WARNING - May be problems with duplicate id_customer in database"); //if > 1 then problem
}
if (![context save:&error])
{
NSLog(@"Whoops, couldn't save Rights : %@", [error localizedDescription]);
}
}
如果答案是我必须通过在iPad上再次写下这种关系这一事实来管理它,我很遗憾地说CoreData似乎比使用SQLite的经典SQL管理提供更多的工作。 如果我没错:
使用SQL,您必须从服务器下载表 包含外键,就完成了。
使用CoreData,您必须管理如果它是一个事实 外键,你必须建立关系。
编辑: 供您参考: 概念数据模型: 我使用Sqlite做我自己的表,然后从服务器直接复制数据(检索主键,外键)。它比使用CoreData好十倍。我使用DB Browser for SQLite来创建我的schéma一次,就是这样。无需编写2000行代码来编写使应用程序复杂化的关系。
结论:我放弃了CoreData!