Assignment 6 - Place not disappearing from table view when no of photos hits 0

24 views
Skip to first unread message

Harry

unread,
Jun 2, 2012, 4:08:34 AM6/2/12
to iphone-appd...@googlegroups.com

Hi there,


I have the following problem.


I can add photos and delete photos by pressing the Visit/Unvisit  button; however, when I press the Unvisit button the photo does disappear from the database, the number of photos decreases, and the photo disappears from the list of photos , BUT when the number of photos hit zero, the place still remains in the database. So I could have, for example, Amsterdam as title and 0 photos as subtitle. Shouldn't the place disappear automatically as well once the number of photos hits 0. Anyone an idea where the problem could be? I do of course use CoreDataTableViewController.[mh] and the FetchedResultsController.


Thanks

Harry

unread,
Jun 7, 2012, 1:42:15 PM6/7/12
to iphone-appd...@googlegroups.com

Yeah, I was thinking along those lines as well or rather I was thinking that this would be hint no. 14. I have in my removePhoto:fromVacationDocument: method the following lines of code 

if (photo) { 

        [vacationDoc.managedObjectContext deleteObject:photo];

// Hint 14

        photo.title = nil

        photo.subtitle = nil

        photo.whereTook = nil; // place is NOT removed 

        photo.whatsWritten = nil; // tag is removed

    }


The problem is that the tag is removed when no. of photos hits 0, but the Place still remains. Just to make 100% sure, my relationships are as follows


Place <--------->>Photo<<----------->>Tag


It's quite frustrating, because everything is working as it supposed to be, except this.



On Thursday, June 7, 2012 7:55:57 AM UTC+2, Sarah wrote:
You have to manually delete the place once its photo count hits zero. Same with the tags. 

Sarah

unread,
Jun 7, 2012, 6:01:55 PM6/7/12
to iPhone Application Development Auditors
I implemented a new method in Photo+Flickr to handle deleting photos,
this is the code I have, where photo is the photo to be deleted:

{
// Manage relationships
Place *place = photo.place;
if ([place.photos count] == 1)
[context deleteObject:place];

NSSet *tags = photo.tags;
for (Tag *tag in tags)
{
if ([tag.photos count] == 1)
[context deleteObject:tag];
}

// Delete photo
[context deleteObject:photo];

NSError *error = nil;
[context save:&error];

if (error)
NSLog(@"[Photo+Flickr] Error deleting Photo object: %@",
[error userInfo]);

done = YES;
}

I think the reason that Core Data doesn't automatically remove a Place
is that if you look at the relationship between Photo and Place, you
see it says "Nullify". That means that every time a photo is deleted,
the corresponding object in the NSSet place.photos is set to null.
After all the photos are removed from the set, the place is left with
an empty set (not nil). The place object also has other attributes
that are non-nil, so Core Data doesn't have any reason to remove it.
It's up to us to know that a place with no photos doesn't need to be
in the database anymore. I think the same applies for the Tag object,
I don't know why yours is being automatically deleted.

Harry

unread,
Jun 8, 2012, 11:32:45 AM6/8/12
to iphone-appd...@googlegroups.com

OK Sarah, this does the trick. I think you could be right about the reason why Core Data does not remove Place as well. Yet, I remain confused about this whole issue. 


Thanks!

Reply all
Reply to author
Forward
0 new messages