Core Data: Unable to store a NSData object into an entity's binary property.
Core Data: Unable to store a NSData object into an entity's binary property.
- Subject: Core Data: Unable to store a NSData object into an entity's binary property.
- From: "Frederick C. Lee" <email@hidden>
- Date: Sun, 8 Jan 2006 12:03:16 -0800
I'm trying to do NSImage I/O from persistent storage. But first I need
to be able to I/O a simple binary file (NSData).
The MOC looks okay before I SAVE; which MOC 'saves' successfully.
All I'm getting is an empty NSSet from a general fetch.
In your earlier messages, you indicated you might be overriding the
accessor methods -- is this still the case?
mmalc
I was trying to simplify the object to tackle just the store-binary
issue. So I had
used a NSManagedObject.
I've re-read and followed some of the references stated.
I've cleanup my code per mmalc's comments and the GVManagedObject.
But I am NOT getting ANY binary storage.
I hope I coded GVManagedObject correctly.
Is there anything else that I'm overlooking?
Ric.
PS: I'm using NSXMLStoreType merely for dev. purposes.
[REGION] --->> [MAP]
Data: 'image' - unknown/transient (NSImage)
'imageData' - binary (NSData)
=============================================================
1) Inserting binary data (NSData) into GVManagedObject
(NSManagedObject):
NSMutableSet *regionMaps = [region mutableSetValueForKey:@"map"];
GVManagedObject *map = [NSEntityDescription
insertNewObjectForEntityForName:@"Map"
inManagedObjectContext:managedObjectContext];
[map setValue:[NSString stringWithFormat:@"%@ Map",inRegion]
forKey:@"name"];
NSData *myData = [NSData dataWithContentsOfFile:
@"/Users/Ric/Library/Application Support/Eccron/Maps/tiff/
Europe1.tif"];
if (myData)
[map setValue:myData forKey:@"imageData"];
[regionMaps addObject:map];
=============================================================
2) Definition of GVMananagedObject:
@interface GVManagedObject : NSManagedObject {
NSImage *image;
NSData *imageData;
}
- (void)setImage:(NSImage *)inImage;
- (NSImage *)image;
- (void)setImageData:(NSData *)inData;
- (NSData *)imageData;
@end
---------------------------------
@implementation GVManagedObject
#pragma mark Accessor Routines
- (void)setImage:(NSImage *)inImage {
[inImage retain];
[image release];
image = inImage;
}
//
------------------------------------------------------------------------
-------------------------------------
- (NSImage *)image {
return image;
}
//
------------------------------------------------------------------------
-------------------------------------
- (void)setImageData:(NSData *)inImage {
[self willChangeValueForKey: @"imageData"];
[self setPrimitiveValue:[inImage copy] forKey: @"imageData"];
[self didChangeValueForKey: @"imageData"];
}
//
------------------------------------------------------------------------
--------------------------------------
- (NSData *)imageData {
NSData *ret;
[self willAccessValueForKey: @"imageData"]; // Need
access to private storage.
ret = [self primitiveValueForKey: @"imageData"];
[self didAccessValueForKey: @"imageData"];
return ret;
}
//================================================================
#pragma mark -
#pragma mark Processing Image Data
// Saving Binary Data to Persistent Storage:
- (void)willSave {
NSImage *tmpImage = [self primitiveValueForKey:@"image"];
[self setPrimitiveValue:[tmpImage TIFFRepresentation]
forKey:@"imageData"];
[super willSave];
}
//
------------------------------------------------------------------------
----------------------------------------
- (void)awakeFromFetch {
imageData = [self valueForKey:@"imageData"];
if (imageData != nil) {
[self setImage:[NSUnarchiver
unarchiveObjectWithData:imageData]];
}
}
@end
========================================================================
=================================================
3) Last mininute check of imageData (NSData binary):
(gdb) po [managedObjectContext insertedObjects]
<NSCFSet: 0x4819370> (<GVManagedObject: 0x4818c60> (entity: Map; id:
0x4818ca0 <x-coredata:///Map/t4741E729-E177-4428-92EB-8C1558597EA4> ;
data: {
city = nil;
country = nil;
image = nil;
imageData = <4d4d002a 00000008 001400fe 00040000 00010000
00000100 00030000 000102a3 00000101 00030000 00010203 00000102 0003>;
memo = nil;
name = "Europe Map";
region = 0x4818090 <x-coredata:///Region/
tF0D082A2-154B-4CE5-8652-D0CDDC5E7B29>;
subregion = nil;
}), <NSManagedObject: 0x4818040> (entity: Region; id: 0x4818090 <x-
coredata:///Region/tF0D082A2-154B-4CE5-8652-D0CDDC5E7B29> ; data: {
country = ();
map = (0x4818ca0 <x-coredata:///Map/t4741E729-
E177-4428-92EB-8C1558597EA4>);
memo = nil;
name = Europe;
}))
========================================================================
=================================================
4) Fetched Result: No imageData object (empty set):
NSSet *fetchedSetName = [[NSSet alloc] initWithSet:[fetchedObject
valueForKeyPath:@"map.name"]];
NSSet *fetchedSetImage = [[NSSet alloc] initWithSet:[fetchedObject
valueForKeyPath:@"map.imageData"]];
(gdb) po fetchedSetName
<NSCFSet: 0x399110> (Europe Map)
(gdb) po fetchedSetImage
<NSCFSet: 0x3a92b0> () <-- *NO DATA*
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden