Save a lot of code by using NSClassFromString in Objective C (一)

2014-11-24 00:59:32 · 作者: · 浏览: 9

Recently, I am implementing database synchronize feature by using SQL sync framework 2.1. In the iOS client, I need save each row of data (entity) into its corresponding table.


From the metadata of the response, I can see the entity table name and its columns' value and name. Usually, I need write method for each table to save the entity into the table, like below piece of code: Status and Priority are the tables' name.

// populates the StatusEntity store with the values from the json object dict
+ (void)populateStatus: (id)dict withMetadata:(id)metadata withContext:(NSManagedObjectContext*) context;
{
bool isTombstone = [Utils isDeleted:metadata];
StatusEntity *status = nil;

NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
[fetch setEntity:[NSEntityDescription entityForName:@"Status" inManagedObjectContext:context]];
[fetch setPredicate:[NSPredicate predicateWithFormat:@"ID = %@", [dict valueForKey:@"ID"]]];
NSArray *results = [context executeFetchRequest:fetch error:nil];
if (results.count == 1)
{
status = [results objectAtIndex:0];
}


if (status == nil && !isTombstone)
{
// insert new status
status = [NSEntityDescription insertNewObjectForEntityForName:@"Status" inManagedObjectContext:context];
}

// if its deleted and we have not seen it ignore it
// else delete it
if (isTombstone && status != nil)
{
[context deleteObject:status];
}
else if (!isTombstone)
{
status.ID = [dict valueForKey:@"ID"];
status.statusName = [dict valueForKey:@"Name"];
}

}

// populates the PriorityEntity store with the values from the json object dict
+ (void)populatePriority: (id)dict withMetadata:(id)metadata withContext:(NSManagedObjectContext*) context;
{
bool isTombstone = [Utils isDeleted:metadata];
PriorityEntity *priority = nil;

NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
[fetch setEntity:[NSEntityDescription entityForName:@"Priority" inManagedObjectContext:context]];
[fetch setPredicate:[NSPredicate predicateWithFormat:@"ID = %@", [dict valueForKey:@"ID"]]];

NSArray *results = [context executeFetchRequest:fetch error:nil];
if (results.count == 1)
{
priority = [results objectAtIndex:0];
}


if (priority == nil && !isTombstone)
{
// insert new status
priority = [NSEntityDescription insertNewObjectForEntityForName:@"Priority" inManagedObjectContext:context];
}


// if its deleted and we have not seen it ignore it
// else delete it
if (isTombstone && priority != nil)
{
[context deleteObject:priority];
}
else if (!isTombstone)
{
priority.ID = [dict valueForKey:@"ID"];
priority.priorityName = [dict valueForKey:@"Name"];
}

}

Thanks NSClassFromString method, which saved me a lot of time and code to just use a method to handle all the entities automatically.


The most important methods are NSClassFromString and insertNewObjectForEntityForName:


Class cls = NSClassFromString(tableName);
if (cls != Nil)
{

// if the name is a class object, then we can call the class method directly.


newObject = [cls findFirstWithPredicate:predicate];
}


and:

if (NSClassFromString(name)) {

// Based on the entity name, w