Sharing an array between 2 classes - iphone

I currently have two arrays, 1 in each class, but I am cloning them before displaying the other viewController. So whatever happens e.g. delete an item in one viewController, I clone the array for the other ViewController when it needs it.
What is the best way to make these ViewControllers read and write to the same array? I would like a shared array resource which I can access, modify from the 2 viewControllers, possibly a third, whenever necessary.
How is this done without cloning all the time.

If the array is shared only by the two view controllers, just let them point to the same object.
#interface FirstViewController {
//...
NSMutableArray *arrayData;
}
#property (nonatomic, retain) NSMutableArray *arrayData;
#end
#interface SecondViewController {
//...
NSMutableArray *arrayData;
}
#property (nonatomic, retain) NSMutableArray *arrayData;
#end
And somewhere in the code
NSMutableArray *array = [[NSMutableArray alloc] init];
firstViewController.arrayData = array;
secondViewController.arrayData = array;
[array release];
This array conceptually becomes a Model for your design. If the two view controllers perform the same tasks on the data of the array that can be abstracted, consider having a custom class that contains the array and serves as the model class.

You can create a singleton class which holds the array, then access it through the sharedInstance. I recommend this article for a review of the various approaches to this situation.

This is probably a good use of the Model-View-Controller paradigm. Break out the array into the model that both view controllers can access.

Related

Regarding NSPredicate and NSMutableArray deep copy

I am trying to copy NSMutableArray which is based of my Registration NSMutableArray Class and trying to filter a boolean. The concern is, since the nsmutablearray is from the class, each time I try to alloc and initwitharray:[self person] man]; where man is a nsmutablearray it doesn't allow me to do it. Is this functionality only localized or can be globally utilized as well? Or am I missing something here?
Thanks.
You have to make sure that "man" is declared as a property of "[self person]". i.e. in the header of the class that [self person] is an instance of,
#property (nonatomic, retain) NSMutableArray *man;
and in the implementation:
#synthesize man;

NSManagedObjectContext executeFetchRequest returns array containing objects that don't stick around

I have a UITableView I'm populating with data from CoreData. I have a data access class I call a method on to get the array of data to populate the table with. In that method I have something like this:
NSArray *fetchedArray = [context executeFetchRequest:request error:&error];
I was just returning this array directly, but was getting an error in my view controller when I try to set its local property that holds the returned array
#property (nonatomic, retain) NSArray *listData;
and
#synthesize listData; // <-- error here -[CFNumber release]: message sent to deallocated instance...
respectively.
It seemed like the 'retain' in my #property was trying to release the previous listData array, which seemed to have already been released (or, more likely an object in the array or one of its properties had been released).
So in my data access class I added the following after the fetchedArray is produced by the context:
NSMutableArray *listArray = [[[NSMutableArray alloc] init] autorelease];
for (Response *item in fetchedArray) {
[listArray addObject:item];
}
return listArray;
But I still get the same error in the #synthesize listData back in the view controller. It doesn't happen the first time usually, but after tapping through to the detail controller and then going back to the list and then reloading the list with different data (e.g. filtering based on user input which calls the data access method to return an updated list - hence the error in the setter for listData).
I'm not entirely sure if my problem is memory management related or related to something I'm not understanding about what the context returns. It'd be nice if a fetch request returned data that didn't get released when I think I've retained it. :(
EDIT Note that given the answer, the title of the question may be a bit misleading.
Ah - just had to think it through a bit more. My problem was that I was assigning one of the objects in the array to a property on the detail controller, but calling release on that property in my detail controller's dealloc.
After changing #property (nonatomic, assign) to #property (nonatomic, retain) it doesn't crash. Yay. Sooo looking forward to ARC.

How do I populate an NSMutableArray in one class with another object?

I know this is a simple answer, but I can't seem to find the solution. I created an object in its own class and I am trying to populate it with data from another class. For simple data types like NSString, I have no problem, but when trying make an NSMutableArray equal to another NSMutableArray or when I try to populate a NSMutableArray with another objects (like strings), I keep getting exception errors...
Here is the object I am trying to populate:
#import <Foundation/Foundation.h>
#interface RSSFeedList : NSObject {
NSString *subject;
NSMutableArray *rssfeedDetail;
}
#property (nonatomic, retain) NSString *subject;
#property (nonatomic, retain) NSMutableArray *rssfeedDetail;
#end
This is how I was able to populate the NSString 'subject' in another class:
rssFeedList.subject = #"test";
However, if I follow similar convention within that same class with respect to an Array, it throws an exception:
rssFeedList.rssfeedDetail = rssItemDetailArray;
Where rssItemDetailArray is a NSMutableArray that I have built in the same class.
I have also tried to add items (i tried strings for testing) to the NSMutableArray directly like so to no avail:
[rssFeedList.rssfeedDetail addObject:#"test"];
Any ideas?? Thanks in advance!!
I'm almost certain that you've forgotten to synthesize rssfeedDetail.

how can I display MSMutableArray data in UITableViewController?

I am getting data from NSTextField, and saving data in NSMutableArray. I want to show the same data in another class which in child of UITableViewController.
How can I show data of NSMutableArray to myTableView which is another class??
Help me out, please
Surely, I will appraise if I found proper way.
Your tableView is in a MyViewController class. You need to create a NSMutableArray *sourceArray attribute on that class, as well as the associated property, using for instance:
#property (nonatomic, retain) NSMutableArray *sourceArray;
Then when you instantiate this View Controller or whenever you make it appear, assign the results to sourceArray :
MyViewController *mvc = [[MyViewController alloc] initWith...];
mvc.sourceArray = theResult;
[self presentModalViewController:mvc animated:YES];
[mvc release];
Then use the sourceArray as the Table View datasource.
Make a property of NSMutableArray in app delegate class and assign the result of the source array to it as soon as you fetch any result, because you can access its instance all over your project.

Shared View Controller access to model class

I am trying to keep close to the MVC approach to programming in an objective C application.
I have a model class and two View Controllers.
#interface Disc : NSObject {
NSString *discType;
NSNumber *capacity; }
#property (nonatomic,retain) NSString *discType;
#property (nonatomic,retain) NSNumber*capacity;
#implementation Disc
#synthesize discType,capacity;
Then for View Controller A
#interface DiscTypeViewController : SecondLevelViewController {
NSString *discTypeSub;
}
#property (nonatomic,retain) NSString *discTypeSub;
#end
#implementation DiscTypeViewController
#synthesize discTypeSub;
Now, I know I can access the members of the model (disc) class from View controller A
Disc *disc1 = [[Disc alloc]init];
[disc1 setDiscType:#"DVD"];
discTypeSub = [disc1 discType];
This returns the value "DVD" which is fine.
The question is, how can my Second View Controller access that same String that returned
"DVD". There's no point in initializing a new instance of Disc. I need the values that were
created from View Controller A calling the setter/getter methods of the Disc class.
What is the best design approach for such a scenario, any info would be much appreciated.
You could create some sort of a context object. Most likely it would be a SingleTon or Factory (static getters/setters). You can have a dictionary with keys #"DVD" or #"BlueRay" with values of type Disk.
Please consider that these objects will reside in your memory while stored in you cache dictionary. Consider to use NSCache if you target iOS4.