Suwitcha Sugthana
Suwitcha Sugthana

Reputation: 1301

fetchedResultsController from 2 entities

I am learning about CoreData and started to use it in one of my project.

I use fetchedResultsController to get data out of CoreData to populate a uitableview just fine.

The problem I am having is that I need to populate 1 table from 2 Entities. This is what I have right now

   NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Contact" inManagedObjectContext:delegate.managedObjectContext];

  [fetchRequest setEntity:entity];

This will only get data from "Contact" Entity but I also need to populate table with data from "Category" Entity as well. And I don't know how to do that.

basically the end result, I would like to have tableview show something like (assume contact has 3 and category has 2 items)

uiTableindex0 :  fetchContact 0

uiTableindex1 :  fetchContact 1

uiTableindex2 :  fetchContact 2

uiTableindex3 :  fetchCategory 1

uiTableindex4 :  fetchCategory 2

Upvotes: 0

Views: 408

Answers (1)

Sudhanshu
Sudhanshu

Reputation: 3960

@Suwitcha Sugthana in this condition i would suggest you to populate the data from two different entities into two different arrays suppose they are are (myArray1 and myArray2) by making there NSFetchRequest objects differently .And print your two arrays on the cell like this....

if(indexPath.row<[myArray1 count])
 {
  cell.text=[myArray1 objectAtIndex:indexPath.row];
 }
 else 
  cell.text=[myArray2 objectAtIndex:(indexPath.row-[myArray1 count])]

//myArray1 has data of contact
//myArray2 has data of catagory

And you will get your required format on the table through this..........Hope this may Help u!!!

Upvotes: 1

Related Questions