1

関連データNSManagedObjectを含むようにシリアライズしようとしています。NSDictionary

ここでそのためのコードをいくつか見つけました: http://vladimir.zardina.org/2010/03/serializing-archivingunarchiving-an-nsmanagedobject-graph/

残念ながら、 のサポートはありませんNSOrderedSet。自分で実装しようとしましたが、メッセージがクラッシュして、セレクターがオンラインで認識されませんif (!relatedObject.traversed) {

- (NSDictionary*) toDictionary
{
    self.traversed = YES;

    NSArray* attributes = [[[self entity] attributesByName] allKeys];
    NSArray* relationships = [[[self entity] relationshipsByName] allKeys];
    NSMutableDictionary* dict = [NSMutableDictionary dictionaryWithCapacity:
                                 [attributes count] + [relationships count] + 1];

    [dict setObject:[[self class] description] forKey:@"class"];

    for (NSString* attr in attributes) {
        NSObject* value = [self valueForKey:attr];

        if (value != nil) {
            [dict setObject:value forKey:attr];
        }
    }

    for (NSString* relationship in relationships) {
        NSObject* value = [self valueForKey:relationship];

        if ([value isKindOfClass:[NSSet class]]) {
            // To-many relationship

            // The core data set holds a collection of managed objects
            NSSet* relatedObjects = (NSSet*) value;

            // Our set holds a collection of dictionaries
            NSMutableSet* dictSet = [NSMutableSet setWithCapacity:[relatedObjects count]];

            for (ExtendedManagedObject* relatedObject in relatedObjects) {
                if (!relatedObject.traversed) {
                    [dictSet addObject:[relatedObject toDictionary]];
                }
            }

            [dict setObject:dictSet forKey:relationship];
        }
        else if ([value isKindOfClass:[NSOrderedSet class]]) {
            // To-many relationship

            // The core data set holds a collection of managed objects
            NSOrderedSet* relatedObjects = (NSOrderedSet *)value;

            // Our set holds a collection of dictionaries
            NSMutableSet* dictSet = [NSMutableSet setWithCapacity:[relatedObjects count]];

            for (ExtendedManagedObject* relatedObject in relatedObjects) {
                if (!relatedObject.traversed) {
                    [dictSet addObject:[relatedObject toDictionary]];
                }
            }

            [dict setObject:dictSet forKey:relationship];
        }
        else if ([value isKindOfClass:[ExtendedManagedObject class]]) {
            // To-one relationship

            ExtendedManagedObject* relatedObject = (ExtendedManagedObject*) value;

            if (!relatedObject.traversed) {
                // Call toDictionary on the referenced object and put the result back into our dictionary.
                [dict setObject:[relatedObject toDictionary] forKey:relationship];
            }
        }
    }

    return dict;
}


- (void) populateFromDictionary:(NSDictionary*)dict
{
    NSManagedObjectContext* context = [self managedObjectContext];

    for (NSString* key in dict) {
        if ([key isEqualToString:@"class"]) {
            continue;
        }

        NSObject* value = [dict objectForKey:key];

        if ([value isKindOfClass:[NSDictionary class]]) {
            // This is a to-one relationship
            ExtendedManagedObject* relatedObject =
                [ExtendedManagedObject createManagedObjectFromDictionary:(NSDictionary*)value
                                                               inContext:context];

            [self setValue:relatedObject forKey:key];
        }
        else if ([value isKindOfClass:[NSSet class]]) {
            // This is a to-many relationship
            NSSet* relatedObjectDictionaries = (NSSet*) value;

            // Get a proxy set that represents the relationship, and add related objects to it.
            // (Note: this is provided by Core Data)
            NSMutableSet* relatedObjects = [self mutableSetValueForKey:key];

            for (NSDictionary* relatedObjectDict in relatedObjectDictionaries) {
                ExtendedManagedObject* relatedObject =
                    [ExtendedManagedObject createManagedObjectFromDictionary:relatedObjectDict
                                                                   inContext:context];
                [relatedObjects addObject:relatedObject];
            }
        }
        else if (value != nil) {
            // This is an attribute
            [self setValue:value forKey:key];
        }
    }
}
4

3 に答える 3

4

それは速くて簡単な方法です

NSMutableArray *array = [NSMutableArray arrayWithCapacity:ManagedObjectItems.count];
[[ManagedObjectItems allObjects] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    Diary_item_food *food = obj;
    NSArray *keys = [[[food entity] attributesByName] allKeys];
    NSDictionary *dict = [obj dictionaryWithValuesForKeys:keys];
    [array addObject:dict];
}];
于 2013-08-01T04:49:22.107 に答える
1

Gihub で準備完了の要点を見つけました: https://gist.github.com/nuthatch/5607405

于 2014-01-30T09:15:09.840 に答える
0

さらに簡単な方法として、objectID を照会し、フェッチ要求で NSDictionaryResultType を使用します。

更新: 関連データが必要ない場合のみ。

于 2014-01-29T20:50:59.413 に答える