I have addPhotos and addPhotosObject methods which were given to me
when I generated the class files from the XCode model, so I thought I
could do it with those, but I haven't had success yet.
So my question is: is it better to create an array before creating my
photo/person objects, use the array to weed out the duplicate person
entries, and then create my objects? Or is there some way to retrieve
the Josh person when I find it exists, and then add the photo to the
set of photos for him?
Thanks,
Bill
This is how I did it:
Parse the plist.data file
create a photo object
assign its name
assign its url
iterate through the people who already exist (do a fetch)
see if the current photos owner matches a person who exists,
if so let that person be the owner
if no person was found, create a new person and link that person
to this photo
Here is a bit of code to help identify if a person already exists:
peopleArray = (NSMutableArray *)[flickr
fetchManagedObjectsForEntity:@"Person" withPredicate:nil];
NSEnumerator *enumerator = [peopleArray objectEnumerator];
Person *person;
BOOL exists = FALSE;
while (person = [enumerator nextObject]) {
if([person.name isEqualToString:[dict objectForKey:@"user"]]) {
exists = TRUE;
NSLog(@"-- Person Exists : %@--", person.name);
[newPhoto setOwner:person];
}
}
Hope this helps,
Wayne
By
Enviado desde mi iPhone
> --
> You received this message because you are subscribed to the Google
> Groups "iPhone Application Development Auditors" group.
> To post to this group, send email to iphone-appd...@googlegroups.com
> .
> To unsubscribe from this group, send email to iphone-appdev-aud...@googlegroups.com
> .
> For more options, visit this group at http://groups.google.com/group/iphone-appdev-auditors?hl=en
> .
>
Wayne, thanks for your explanation and code sample as well. So in your
example, if the person exists, you'd simply attach the photo to that
person using [newPhoto setOwner:person]?
For that matter, did you even worry about Josh being in the Person
table twice? I'm trying to prevent his name showing up twice, but
maybe it doesn't matter. My gut says it does.
I know how I'd do this in php/mysql, but I feel like a newborn in
handling it via Core Data. I'll keep noodling with it.
Bill
Bill - Here is the next part of code, where I create the person if
they don't already exist:
// if person does not already exist then add the person
if (!exists) {
Person *newPerson = (Person *)[NSEntityDescription
insertNewObjectForEntityForName:@"Person"
inManagedObjectContext:managedObjectContext];
// create the person
[newPerson setName:[dict objectForKey:@"user"]];
NSLog(@"-- Person Created : %@--", newPerson.name);
// associate the person with the photo
[newPhoto setOwner:newPerson];
}
I personally don't like the idea of having duplicates, and like you, I
wanted to have them show up only once.
Core Data = as with all abstraction layers at some point common sense
also gets abstracted :-)
Good Luck,
Wayne
Thanks so much! It turns out my problem was one of logic--I didn't
think I could (or would need to) do a fetch on the database when I was
adding things to the database. Now I realize it's the only way to get
the Person object if person already exists. That was the missing
piece! :-)
Ok, time to clean up the remaining little things, and then FINALLY I
can move on to Paparazzi3!
Thanks again,
Bill