3

I have a very simple app that has one CoreData Entity with a circular relationship to itself. I want to use this data as a plist in another app.

The relationship is 'to one' on the parent side & 'to many' on the child side.

Basically a tree with one item at the top and then children of that object, children of the child objects, etc...

I want to create a dictionary/pList from this data with the structure of the entity including the relationships. (Does that make sense?)

So far, with the help of other answers here, I can get a plist of all the objects (but all on the same "level") or a plist of the ultimate parent object (but the children relationship is faulted).

This is what I'm doing to try and get the data and save it to my desktop:

    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:ENTITY_NAME];
    request.resultType = NSDictionaryResultType;

    NSError *fetchError = nil;
    NSArray *results = [self.databaseDocument.managedObjectContext executeFetchRequest:request error:&fetchError];

    NSLog(@"%@", results);

    NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString *path = [caches stringByAppendingPathComponent:@"/../../../../../../../../Desktop/my.plist"];
    [results writeToFile:path atomically:NO];

At the moment there are only three objects. A main object, a child of that object, and a child of that child object.

This is giving me an plist which is an array with three objects in it.

If I include a predicate to only get the object that has no parent like this:

    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:ENTITY_NAME];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"parent == nil"];
    request.predicate = predicate;

    NSError *fetchError = nil;
    NSArray *results = [self.databaseDocument.managedObjectContext executeFetchRequest:request error:&fetchError];

    NSLog(@"%@", results);

    Item *mainItem = [results lastObject];

    NSArray *keys = [[[mainItem entity] attributesByName] allKeys];
    NSDictionary *dict = [mainItem dictionaryWithValuesForKeys:keys];

    NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString *path = [caches stringByAppendingPathComponent:@"/../../../../../../../../Desktop/my.plist"];
    [dict writeToFile:path atomically:NO];

This gives me a directory but does not include the children.

Can anyone explain to me how I can get the plist structured like my entity is? In other words just one Directory with all the children contained within, and the children of each child within the Directory for that child, and so on...?

4

1 回答 1

3

There may well be a better way to export your Core Data entities to a plist while preserving the hierarchical structure of the relationships - I'm willing to be educated. I would approach processing them in a recursive method like this: (this is completely untested - meant only for illustrative purposes - also I'm not completely sure how you want the plist structured, etc.)

- (void)processEntity:(NSManagedObject *)object forMutableArray:(NSMutableArray *)mutableArray {
    NSMutableDictionary *entityInfo = [NSMutableDictionary new];

    // populate entityInfo with your object's properties
    // however you are doing that now...


    //  if this object has children - process them
    NSMutableArray *childArray = [NSMutableArray new];
    for( id child in object.children ) {
        [self processEntity:child forMutableArray:childArray];
    }

    [entityInfo setObject:childArray forKey:@"children"];
    [mutableArray addObject:entityInfo];
}

So, you start the process by iterating over the top-level objects (those with no parent) then process them in processEntity:forMutableArray:

于 2012-10-06T12:06:51.920 回答