【问题标题】:NSManagedObject outside of a managedObjectContextNSManagedObject 在 managedObjectContext 之外
【发布时间】:2011-07-25 18:31:57
【问题描述】:

我已经seen others 询问如何在 managedObjectContext 之外使用 NSManagedObject。 似乎每个人都说您不应该这样做,但我找不到有关该做什么的信息。

我实际上是在尝试对我的 NSManagedObject 上设置的数据做两件不同的事情。我想要 将它保存到persistentStore,我想将它发送到远程服务器。我的想法是分配/初始化 我的 NSManagedObject 的一个实例,填充它的属性,然后将其传递给那些属性所在的函数 将被转移到正确实例化的 NSManagedObject,然后将其传递给另一个函数 负责将数据发送到服务器。

在代码中:(Event 是 NSManagedObject 的子类)

// 在我的视图控制器中 事件 *event = [事件分配] init]; event.propertyA = @"foo"; event.propertyB = @"bar"; [自我日志事件:事件]; [自我发送事件:事件]; ---------------------------------- // 视图控制器中的方法 - (void)logEvent(Event *)event { // 我的想法是获取我手动创建的事件,并将其用于 // 在 managedObjectContext 中设置 Event 对象的属性。 事件 *eventEntity = [NSEntityDescription insertNewObjectForEntityForName:@"Event" inManagedObjectContext:self.managedObjectContext]; eventEntity.propertyA = event.propertyA; eventEntity.propertyB = event.propertyB; ... [self.managedObjectContext 保存:&错误]; } -(无效)发送事件:(事件*)事件{ // 将完全相同的事件属性发送到远程服务器 }

如您所料,这在第二行失败,我尝试设置 propertyA。

我应该怎么做?我应该创建一个具有确切的 NSObject 的香草子类吗? 与我的 NSManagedObject 对象相同的属性/属性?我链接到的问题中提出的解决方案是关于 NSInMemoryStoreType,但是当我真正想要的只是一种传递对象的便捷方式时,这似乎有点过头了。只是在这种情况下,我的对象是一个 NSManagedObject,所以我能用它做些什么。

【问题讨论】:

  • 当你说“这失败了”时,到底发生了什么?
  • 它失败了:'NSInvalidArgumentException',原因:'-[Event setTimestamp:]: unrecognized selector sent to instance 0x1c0d50' 因为所有属性都是@dynamic 而不是@syntehesize[d]。

标签: iphone objective-c core-data nsmanagedobject nsmanagedobjectcontext


【解决方案1】:

我不久前在NSManagedObject 上写了一个类别,它创建了一个托管对象的NSDictionary 表示。您可以在托管对象上下文之外使用NSDictionary免责声明:此代码未经彻底测试,另外请注意,它只处理托管对象的属性,不处理关系 .

NSManagedObject+CLDAdditions.h
------------------------------

@interface NSManagedObject (CLDAdditions)
- (NSDictionary*)cld_dictionaryRepresentation;
@end

NSManagedObject+CLDAdditions.m
------------------------------

@implementation NSManagedObject (CLDAdditions)

- (NSDictionary*)cld_dictionaryRepresentation
{
    // Create empty dictionary
    NSMutableDictionary *objectDictionary = [NSMutableDictionary dictionary];
    // Set the entity
    [objectDictionary setObject:[[self entity] name] forKey:@"entity"];
    NSDictionary *attributeKeys = [[self entity] attributesByName];
    // Go through each of the attributes and add them to the dictionary
    for (NSString *attributeKey in attributeKeys) {
        id attributeValue = [self valueForKey:attributeKey];
        if (attributeValue) {
            // Supported objects
            if ([attributeValue isKindOfClass:[NSNumber class]] || [attributeValue isKindOfClass:[NSString class]] || [attributeValue isKindOfClass:[NSNull class]] || [attributeValue isKindOfClass:[NSDate class]] || [attributeValue isKindOfClass:[NSData class]] || [attributeValue isKindOfClass:[NSURL class]]) {
                [objectDictionary setObject:attributeValue forKey:attributeKey];
            // Unsupported objects
            } else if ([attributeValue isKindOfClass:[NSDictionary class]] || [attributeValue isKindOfClass:[NSArray class]]) {
                [NSException raise:NSGenericException format:@"Objects of type \"%@\" are not supported as Core Data attributes.", [attributeValue class]];
            // Transformable objects (conforming to NSCoding)
            // TODO: Add support for custom value transformers
            } else if (([[attributeKeys objectForKey:attributeKey] attributeType] == NSTransformableAttributeType) && [attributeValue conformsToProtocol:@protocol(NSCoding)]) {
                NSData *attributeData = [NSKeyedArchiver archivedDataWithRootObject:attributeValue];
                [objectDictionary setObject:attributeData forKey:attributeKey];
            // Otherwise raise an exception
            } else {
                [NSException raise:NSGenericException format:@"Unsupported object type \"%@\". Objects must conform to the NSCoding protocol.", [attributeValue class]];
            }
        }
    }
    return objectDictionary;
}

@end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多