Re: NSData as value in NSManagedObject
Re: NSData as value in NSManagedObject
- Subject: Re: NSData as value in NSManagedObject
- From: Mikkel Eide Eriksen <email@hidden>
- Date: Sat, 20 Feb 2010 16:29:59 +0100
Hi Jerry
Thanks for the reply, here are the methods in question. Bear in mind
I'm still new to Core Data so there are probably some grievous things
going on (the project is GC, by the way).
addXML:toBucket:atPath: gets called when self receives new XML. I
choose from the added XML via an NSBrowser.
- (void)addXML:(NSXMLDocument *)xml toBucket:(NSString *)bucket atPath:
(NSString *)path
{
NSManagedObject *child = [self addOrGetChildAtPath:path
inBucket:bucket];
[child setValue:[xml XMLData] forKey:@"xml"];
}
- (IBAction)browserSelectionMade:(id)sender
{
if (![[sender selectedCell] isLeaf])
return; //only interested in leaf nodes
if ([[treeController selectedObjects] valueForKey:@"xml"] == nil)
return; //no XML in here - maybe throw an error?
NSData *xmlData = (NSData *)[[[treeController selectedObjects]
valueForKey:@"xml"] lastObject]; //HERE'S THE PROBLEM
NSXMLDocument *xmlDoc = [[NSXMLDocument alloc] initWithData:xmlData
options:NSXMLDocumentTidyXML
error:nil];
[aoImageController loadXML:xmlDoc];
}
- (NSManagedObject *)addOrGetChildAtPath:(NSString *)path inBucket:
(NSString *)bucketName
{
NSEntityDescription *aoEntity = [NSEntityDescription
entityForName:@"AOItemZ" inManagedObjectContext:[appDelegate
managedObjectContext]];
NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init]
autorelease];
[fetchRequest setEntity:aoEntity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(name
like %@) AND (parent == nil)", bucketName];
[fetchRequest setPredicate:predicate];
NSManagedObject *bucket = nil;
NSManagedObject *child = nil;
NSArray *fetchResults = [[appDelegate managedObjectContext]
executeFetchRequest:fetchRequest error:nil];
//select or create bucket:
if ([fetchResults count]) {
bucket = [fetchResults lastObject];
} else {
bucket = [NSEntityDescription
insertNewObjectForEntityForName:@"AOItemZ" inManagedObjectContext:
[appDelegate managedObjectContext]];
[bucket setValue:bucket forKey:@"name"];
}
//create or select existing node at path in bucket:
for (NSString *pathComp in [path pathComponents]) {
BOOL foundChild = NO;
for (child in [bucket valueForKey:@"children"]) {
if ([[child valueForKey:@"name"] isEqualTo:pathComp]) {
foundChild = YES;
break;
}
}
if (!foundChild) {
child = [NSEntityDescription
insertNewObjectForEntityForName:@"AOItemZ" inManagedObjectContext:
[appDelegate managedObjectContext]];
[child setValue:pathComp forKey:@"name"];
NSMutableSet *childSet = [bucket mutableSetValueForKey:@"children"];
[childSet addObject:child];
[child setValue:bucket forKey:@"parent"];
}
bucket = child; //for next iteration
}
return child;
}
On 20/02/2010, at 15.27, Jerry Krinock wrote:
On 2010 Feb 19, at 01:04, Mikkel Eriksen wrote:
I've put a small NSXMLDocument into a Core Data binary property
like this:
code:
NSManagedObject *object = /* ... */
NSXMLDocument *xmlDoc = /* ... */
[object setValue:[xmlDoc XMLData] forKey:@"xml"];
Mikkel,
That doesn't make sense. If you're still having trouble you should
post some more code, actual code.
Jerry
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden