-2

I have a UIViewController called BaseController, this controller holds all the functionality of the screen, download data from service and more...

I'm currently inside a UIViewController called HomeVC which inherit from BaseController, when initialize this UIViewController I'm calling a method to download the data from service, after downloading the data, I insert the data (lets call the object DbHomeData) into CoreData and display it in a UITableView on HomeVC.

My problem occurs when I click on one of the UITableView cell's, it pushes a new UIViewController that inherit also from BaseController.

This new UIViewController is downloading new data and insert it once more to CoreData as the HomeVC is doing, but now I have the old data and new downloaded data together, but if I will remove the old data, then when I pop this UIViewController, the old data will be gone and the UITableView will display nothing.

How can I workaround this issue or fix it? Is there a way to initialize NSManagedObject without inserting it into CoreData? Is there a better solution?

Thanks in advance!

diagram

ytpm
  • 4,962
  • 6
  • 56
  • 113

1 Answers1

1

Is there a way to initialize NSManagedObject without inserting it into CoreData?

Yes you can do it like this:

NSManagedObject *myObj = [[NSManagedObject alloc] initWithEntity:myEntity insertIntoManagedObjectContext:nil];

This instantiates a new NSManagedObject but doesn't associate it with a context.

Is there a better solution?

Hard to say. You are not giving us much to go on but I think your main issue is in the inheritance structure you've set up for yourself. Think about putting your downloading code in a separate model class (e.g. MyDownloadManager), not in a controller class. Keep the MVC design pattern in mind.

Rob Sanders
  • 5,197
  • 3
  • 31
  • 58
  • The downloading code is in a separate model class called `DownloadManager`, but I'm calling it when the `BaseController` is initialize. My only problem is what to do with already downloaded data after I initialize the same class twice? The are both `BaseController` so they have the same lifecycle, which is download data from service, but each download with different params. The objects are inserted to the same `CoreData` `Entity`. – ytpm Mar 15 '15 at 10:44
  • What to do with already downloaded data? You can save it into CoreData and refetch it when your controller appears. Or you can get rid of it. Do you want to keep the data or not? Also I wouldn't put the download code in the superclass but put it in each subclass as and when you want. – Rob Sanders Mar 15 '15 at 10:47
  • The issue here is not the `Class` or `SuperClass`, the issue here, is that the downloaded data is saved into `CoreData`, but I just want to save the first downloaded data, not the second. The second data is after I called the `WebService` request for filtered data, but this data is only for filtered results controller. Then they are both saved into `CoreData` and I get duplicated data in the same `Entity`. – ytpm Mar 15 '15 at 11:09
  • Also when I try to initialize `NSManagedObject` without context, it crashes. – ytpm Mar 15 '15 at 11:17
  • I have used the code above lots of times to instantiate `NSManagedObject`s without inserting them into the database. Check out [this SO question](http://stackoverflow.com/questions/3256195/how-to-deal-with-temporary-nsmanagedobject-instances) for more details. – Rob Sanders Mar 15 '15 at 11:39
  • If you keep getting odd behaviour, use an `NSObject` subclass for the temporary data. – Rob Sanders Mar 15 '15 at 11:40
  • I guess that this is what I will end up doing. I just wanted to know if there is a better more efficient way to do so. Also if what I did is ok or not. – ytpm Mar 15 '15 at 11:58
  • I've added an image, please check it out. – ytpm Mar 15 '15 at 12:01
  • 1
    I'm assuming that the FilteredVC just filters the data downloaded by the BaseVC and the HomeVC displays all the data unfiltered? If so then I would just download in the HomeVC not the BaseVC and then in the FilteredVC I would fetch the data from the context and then filter it, rather than downloading it again. (P.S. I'm assuming quite a lot here because I don't know if what you're downloading in the Home and FilteredVC is the same.) – Rob Sanders Mar 15 '15 at 14:20
  • I assume, that it would work ;) haha, thanks man for everything! :) – ytpm Mar 15 '15 at 16:52