7

I have an iOS app that utilizes RestKit 0.20.1 to retrieve data from a Restful web service. I should also add the app uses CoreData. When the app is started the main screen is a collection view that is populated by a default search term. There is also a textfield in the header that is used as a search bar.

I am having trouble clearing out the previously loaded cells when the user uses the search bar. It just loads the new cells and pushes the previous ones down. Here is the applicable code.

- (BOOL) textFieldShouldReturn:(UITextField *)textField {

//This sets up the NSDictionary for the Restkit postObject parameters
    NSArray *objects =[NSArray arrayWithObjects:textField.text, nil];
    NSArray *keys =[NSArray arrayWithObjects: @"query",nil];
    NSDictionary *params = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
    self.search=params;

//This is the POST request to the server
    [[RKObjectManager sharedManager] postObject:nil path:@"/rest/search?ip=255.255.255.0" parameters:search success:nil failure:nil];

//This is what I thought would clear out the old and replace with the new    
    [self.collectionView reloadData];    

    [textField resignFirstResponder];
    return YES; 
}

I referenced this question How to remove all items and add new items to UICollectionView? and [collectionView reloadData] was the accepted answer.

I chose the textFieldShouldReturn: method from this tutorial. I have also referenced Inserting, Deleting, and Moving Section Items in the apple developer library but I'm not quite sure how to implement the delete items methods.

Any help would be great. This is clearly a rookie question so code snippets are VERY helpful.

UPDATE: Here is how I got it to work.

Added a call to the deleteAllEntitiesForName method shown below before the postObject method and [self.collectionView reloadData] statements in the code above.

- (void) deleteAllEntitiesForName:(NSString*)entityName {
    NSManagedObjectContext *moc = [self managedObjectContext];
    NSEntityDescription *entityDescription = [NSEntityDescription
    entityForName:entityName inManagedObjectContext:moc];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entityDescription];
    NSError *error = nil;
    NSArray *array = [moc executeFetchRequest:request error:&error];
    if (array != nil) {
        for(NSManagedObject *managedObject in array) {
            [moc deleteObject:managedObject];
        }
        error = nil;
        [moc save:&error];
    }

}

I got it from here: Correct way to refresh Core Data database

Community
  • 1
  • 1
Ben
  • 967
  • 3
  • 9
  • 23
  • Is your data overwritten on the previous cell? – AtWork Jun 26 '13 at 04:06
  • I don't believe so. It is as if the previous data is maintained and the new results are added to the collectionView. For example when the app is loaded initially there is 10 individual cells and after a search is done the 10 original remain and 10 new are loaded. – Ben Jun 26 '13 at 04:21
  • put some code of cellforrowindexpath for collectionView-cellForItemAtIndexPath. – AtWork Jun 26 '13 at 04:34
  • You need to check your array count before reloading. – AtWork Jun 26 '13 at 04:36
  • Thanks Abhishek, the array count is not being reset before I reload the collectionView. Do you know how I can clear out the previous data before I perform the reload? – Ben Jun 26 '13 at 04:53

2 Answers2

3

From experience with UICollectionView (not with RestKit), you probably need to clear out your data source before calling reloadData. The reason you're having this effect is that the source that you use to determine the number of items and sections in the collection view still has old data in it.

A simple way to verify this is to NSLog the contents of the data source just before calling reloadData.

Hope this helps!

architectpianist
  • 2,562
  • 1
  • 19
  • 27
  • Definitely helps. Watched the count in my numberOfItemsInSection: method and noticed the first load is 23 then after a search is entered the first pass thru the method is 23 again rather than zero. Then after cycling thru the new data it goes to 48. Any suggestions on how to clear out the data before the reload? – Ben Jun 26 '13 at 04:50
  • Check these answers: http://stackoverflow.com/questions/1077810/delete-reset-all-entries-in-core-data, http://stackoverflow.com/questions/4910063/correct-way-to-refresh-core-data-database – architectpianist Jun 26 '13 at 14:18
  • Thanks architectpianist! I came across the first link last night after I realized my CoreData Entities were persisting the original search. Hadn't found the second one though but it looks like exactly what I'm wanting to do. I'll give it a shot this evening and give an update. – Ben Jun 26 '13 at 15:19
  • The second link was and easy trick. Worked perfectly thanks! – Ben Jun 26 '13 at 22:37
2

If you are using NSMutableArray then you can simply do:

[array_name removeAllObjects];

Do this before adding new objects to the array.

shim
  • 9,289
  • 12
  • 69
  • 108
AtWork
  • 1,283
  • 1
  • 14
  • 34
  • Let me know if you face some problem. – AtWork Jun 26 '13 at 05:07
  • I'm not sure it's that strait forward. The data is stored in CoreData entities so I need to clear them out. Thanks for the response though, your suggestion made it click in my head that the data is being persisted in CoreData rather than an Array. – Ben Jun 26 '13 at 05:14
  • Abhishek, Thanks for helping me realize my CoreData was persisting my previous search results. Its working now with the update I added to the question. – Ben Jun 26 '13 at 22:39