1

我有seen others詢問如何在managedObjectContext之外使用NSManagedObject。 似乎每個人都說你不應該這樣做,但我找不到相關信息。NSManagedObject在managedObjectContext之外

我基本上試圖用我在NSManagedObject上設置的數據做兩件不同的事情。我想要 將其保存到persistentStore,並且我想將它發送到遠程服務器。我的想法是分配/初始化 我NSManagedObject的一個實例,填充它的屬性,然後傳遞到這些屬性 將被轉移到一個正確實例NSManagedObject的功能,然後將它傳遞給另一個函數 將負責用於將數據發送到服務器。

在代碼:(事件是NSManagedObject的子類)

 
// in my view controller 
Event *event = [Event alloc] init]; 
event.propertyA = @"foo"; 
event.propertyB = @"bar"; 

[self logEvent:event]; 
[self sendEvent:event]; 

----------------------------------- 

// method in view controller 
- (void)logEvent(Event *)event { 
    // my thought was to take the event that I manually created, and use it to 
    // set the properties on the Event object in the managedObjectContext. 

    Event *eventEntity = [NSEntityDescription insertNewObjectForEntityForName:@"Event" inManagedObjectContext:self.managedObjectContext]; 

    eventEntity.propertyA = event.propertyA; 
    eventEntity.propertyB = event.propertyB; 
    ... 
    [self.managedObjectContext save:&error]; 
} 

- (void) sendEvent:(Event *)event { 
    // send exact same event properties to remote server 
} 

正如你所期望的,這是第二行,在那裏我嘗試設置propertyA失敗。

我該怎麼做呢?我應該創建一個具有與我的NSManagedObject對象相同的屬性/屬性的精確 的NSObject的vanilla子類嗎?我提出的問題鏈接到的解決方案談到NSInMemoryStoreType,但是當我真正想要的只是一種方便的方式來傳遞一個對象時,看起來似乎過分了。只是在這種情況下,我的對象是一個NSManagedObject,所以我受限於我可以用它做什麼。

+0

當你說「這是失敗」,究竟發生了什麼? – Anomie 2011-03-30 02:46:32

+0

由於'NSInvalidArgumentException',原因:' - [Event setTimestamp:]:無法識別的選擇器發送到實例0x1c0d50',因爲所有屬性@dynamic而不是@syntehesize [d]。 – djibouti33 2011-03-30 03:23:18

回答

0

我上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 
相關問題