Managing duplicate person(s)

0 views
Skip to first unread message

Bill

unread,
Mar 9, 2010, 2:27:25 PM3/9/10
to iPhone Application Development Auditors
I've gotten most of the code doing what it should be, so I've come
back to this issue. In a nutshell, when I read in the FakeData.plist
file, my database (and tableView) show "Josh" twice. In the
appDelegate, when I check that the database exists and create it if
necessary, it seems like that is where I should check for duplicates.
I have the check working, but I can't figure out how to add the 2nd
photo to the NSSet for the already-existing Josh person.

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

Wayne

unread,
Mar 9, 2010, 4:21:54 PM3/9/10
to iPhone Application Development Auditors
Hi 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

Pedro Mu?oz

unread,
Mar 9, 2010, 5:19:37 PM3/9/10
to iphone-appd...@googlegroups.com
Hi i think it's etter to use a Predicate to find out if the person
exists yet in database, with 3 récords your solution maybe is ok but
with 500 or more you must check every récord in each insert.

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
> .
>

Bill

unread,
Mar 9, 2010, 5:35:51 PM3/9/10
to iPhone Application Development Auditors
Hi Pedro, this is happening at startup though, the first time the app
is installed and run, where it has no database yet and needs to create
it and load it from the plist file they provided. So basically I'm
trying to prevent it from inserting the same record twice into the
Person table, and to make sure that all the multiple photos are linked
to the person properly.

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

Wayne

unread,
Mar 9, 2010, 6:04:59 PM3/9/10
to iPhone Application Development Auditors
Pedro - You make a valid point, though to be fair I feel that using
Core Data in the first place is overkill for the task of managing 2
users and 3 photos :-)

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

Bill

unread,
Mar 9, 2010, 7:22:26 PM3/9/10
to iPhone Application Development Auditors
Wayne and Pedro,

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

Reply all
Reply to author
Forward
0 new messages