I have a problem concerning the CoreData and the synchronisation with a server database. When I synchronize two entities with two table on the server, the problem is that if there is a foreign key on the server, how can I manage it ? For example :
//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"];
[badges setValue:[NSNumber numberWithInt:[[object valueForKey:@"id_right"] intValue]] forKey:@"id_right"];
}
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]];***
}
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]);
}
}
If the answer is that I have to manage that by writing the fact that it is a relationship again on the iPad, I am sorry to say that CoreData seems to give more work than the classic management with SQLite with SQL.
Aucun commentaire:
Enregistrer un commentaire