Question

I think this is probably a simple fix, but I am struggling to find the right solution I have the following:

ContactList - a UICollectionView that loads the content with viewDidAppear: method. When a item is selected in the collection view I fire this method:

[self performSegueWithIdentifier:@"pushContactDetail" sender:self];

So far so good. Everything works great. However when I navigate back from the ContactDetail page using the Navigation Controller button my ContactList duplicates the content.

Is there something I should be doing to prevent the viewDidAppear: from running again?

I don't think I want to set the collection to nil when I push the ContactDetail page as that would cause the content to be reloaded each time...

Here is the code:

-(void) viewDidAppear
{
    [super viewDidAppear];
    [self.view setBackgroundColor:myColorLightGrey];
    _contactList = [[NSMutableArray alloc] init];
    [self displayLoadingAndDisableTableViewInteractions];
    [self queryData];
}


- (void) queryData
{    
    //Find the Data
     PFQuery *query = [PFUser query];
     PFUser *consumer = [PFUser currentUser];
    [query includeKey:User_followers];
    [query whereKey:@"email" equalTo:consumer.email];
    query.maxCacheAge = 60 * 60 * 24;  // One day, in seconds
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
    {
         for (PFUser *tmpConsumer in objects)
         {
             for (PFUser *publisher in [tmpConsumer objectForKey:User_followers])
             {
                 [_contactList addObject:publisher];
             }
         }
         [_collectionView reloadData];
         [self hideLoadingAndEnableTableViewInteractions];
    }];
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"contactCell";
    ContactCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    //SNIPPED REMAINDER OF CODE
    ...
}
Was it helpful?

Solution

In your queryData method, nil out the array first

_contactList = nil;
_contactList = [NSMutableArray array]; // or [[NSMutableArray alloc] init];

Move the alloc/init method for _contactList like this:

- (void) queryData
{    
    _contactList = nil;
    _contactList = [[NSMutablArray alloc] init];

    //Find the Data
     PFQuery *query = [PFUser query];
     PFUser *consumer = [PFUser currentUser];
    [query includeKey:User_followers];
    [query whereKey:@"email" equalTo:consumer.email];
    query.maxCacheAge = 60 * 60 * 24;  // One day, in seconds
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
    {
         for (PFUser *tmpConsumer in objects)
         {
             for (PFUser *publisher in [tmpConsumer objectForKey:User_followers])
             {
                 [_contactList addObject:publisher];
             }
         }
         [_collectionView reloadData];
         [self hideLoadingAndEnableTableViewInteractions];
    }];
}

OTHER TIPS

Navigate back with Animation true. It will solve your problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top