0

I'll start by saying I'm new to Objective-C and iPhone but I have history in C++, Java, AS3, ...

I'm trying to build some sort of RSS Reader, and I have an array for all my feeds. How is the best approach to save new feeds to this array? I have a navigation based project, and I have an add button which pushes a viewController on top to enter new feed URL.

But how do I save this back to the array in my other ViewController? Do I need to research more into SQLLite? Or set some delegates? Or Core Data?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Wouter
  • 1

3 Answers3

1

I prefer the singleton method myself but Apple recommends dependency injection i.e. passing a data model object from view controller to view controller as needed.

If you look at a Core Data utilizing template navigation project in Xcode, you can see how this works. The managedObject context is the data model and it is initialized and held by the app delegate. You can then access it two ways:

(1) Since the Application instances itself is a singleton, you can ask it for its delegate and then ask the delegate for its managedObjectContest property. So, in a view controller you would have a property managedObjectContext with a custom getter defined like:

(NSManagedObjectContext *) managedObjectContext{
    if (managedObjectContext !=nil){
        return managedObjectContext;
    }
    // this is basically applicationObject.delegate.managedObjectContext
    self.managedObjectContext=[[[NSApplication sharedApplication] delegate] managedObjectContext];
    return managedObjectContext
}

(2) Alternatively, whenever a view opens another view, it just sets the next view's managedObjectContext property to it's own. So that every view opens with a context. This is useful if you actually have multiple data objects for some reason.

If your just using an array or a custom data model class just substitute its name for the managedObjectContext in the code above.

TechZen
  • 64,370
  • 15
  • 118
  • 145
0

Check out this question. I recommend using a singleton class and creating some listener pattern to signal when the data has changed (or just reload, always, before your view becomes visible).

Community
  • 1
  • 1
ThomasRS
  • 8,215
  • 5
  • 33
  • 48
0

You might want to store your feed items in memory by using the a singleton

Something similar to what is being used: Singleton shared data source in Objective-C

Community
  • 1
  • 1
JFoulkes
  • 2,429
  • 1
  • 21
  • 24