Shared View Controller access to model class - iphone

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.

Related

Can I use a custom initializer for a core data model object?

I use Core Data and have an object ExerciseForRoutine. I'm currently manually creating it and then settings it's attributes, which seems to waste code. Is there any way I can create a custom init method to handle this in one line (I know how to do around alloc/init, but core data has a different init method..)
Current Code:
ExerciseForRoutine *exerciseForRoutine = (ExerciseForRoutine *)[NSEntityDescription insertNewObjectForEntityForName:#"ExerciseForRoutine" inManagedObjectContext:managedObjectContext];
exerciseForRoutine.name = self.selectedExercise;
exerciseForRoutine.timeStamp = date;
exerciseForRoutine.muscleGroup = self.muscleName;
exerciseForRoutine.musclePicture = self.muscleURL;
ExerciseForRoutine Class
#class Routine;
#interface ExerciseForRoutine : NSManagedObject {
#private
}
#property (nonatomic, strong) NSDate * timeStamp;
#property (nonatomic, strong) NSString * name;
#property (nonatomic, strong) NSString * muscleGroup;
#property (nonatomic, strong) NSString * musclePicture;
#property (nonatomic, strong) Routine * exerciseToRoutine;
#end
#implementation ExerciseForRoutine
#dynamic timeStamp;
#dynamic name;
#dynamic muscleGroup;
#dynamic musclePicture;
#dynamic exerciseToRoutine;
I did this using awakeFromInsert and awakeFromFetch.
From Apple's documentation:
In a typical Cocoa class, you usually override the designated initializer (often the init method). In a subclass of NSManagedObject, there are three different ways you can customize initialization —by overriding initWithEntity:insertIntoManagedObjectContext:, awakeFromInsert, or awakeFromFetch. You should not override init. You are discouraged from overriding initWithEntity:insertIntoManagedObjectContext: as state changes made in this method may not be properly integrated with undo and redo.
https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/CoreData/Articles/cdManagedObjects.html
The classes which Xcode creates for handling core data objects should not be overridden, instead what you could do is create your own custom class which inherits from NSObject and write your methods to handle the managed object their.
Sol: You can do this with the help of the parameterized init method
Then it would look something like this
CoreDataHelperClass *someobj = [[CoreDataHelperClass alloc]initWithname:#"name" andTimeStamp:#"Time" andMuscleGroup:#"musclegroup" andPicture:UIImagePNGRepresentation(someimageObj)];
To do the above you need to add your own init method in the CoreDataHelperClass class like this
.h part of CoreDataHelperClass
- (id)initWithName:(NSString*)name andTimeStamp:(NSString*)timeStamp andMuscleGroup:(NSString*)group andPicture:(NSData*)imageData;
.m part of CoreDataHelperClass
- (id)initWithName:(NSString*)name andTimeStamp:(NSString*)timeStamp andMuscleGroup:(NSString*)group andPicture:(NSData*)imageData
{
//you assignment code to the core data attributes goes here
ExerciseForRoutine *obj = [[ExerciseForRoutine alloc]init];
obj.name = name;
obj.timestamp = timeStamp;
//and so on
return self;
}
Anyways what you could also do is pass a dictionary with the keyvalue pair get the values in your custom class or you may also pass an NSMutableArray like what ever suits your business model both will work.
You can get the values of Dictionary or Array inside your CoreDataHelperClass and assign those values to your attribute.
Hope i have got your query right if not then kindly mention the error part via comments
To add to #Radix's answer, you should consider using mogenerator because it'll do much of that subclassing business for you.
http://rentzsch.github.io/mogenerator/
See here for a guide to set it up and have it running on XCode 5.
There's a small caveat to watch out for though: if you get an assertion failure that reads:
-[MOGeneratorApp setModel:] blah blah blah
Then you should point mogenerator to the .xcdatamodel file inside of the .xcdatamodeld package in your Run Script Phase, like so:
mogenerator -m Model.xcdatamodeld/Model.xcdatamodel -O Project/Model --template-var arc=true
Where Project is the name of your project and Model is the name of your model.
See https://github.com/rentzsch/mogenerator/issues/169.

clarifying on properties in objective C

Sorry for the simple question.
When I see a definition of a property inside the h file, but outside of the class #interface scope, what does it mean ?
#property (nonatomic, readonly) RMMapContents *mapContents;
Here is the code:
#class RootViewController;
#class RMMapContents;
#interface MapTestbedAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
//MAIN VIEW
//==============
RootViewController *rootViewController;
// NETWORK DATA
// =============
NSMutableArray *photoTitles; // Titles of images
NSMutableArray *photoSmallImageData; // Image data (thumbnail)
NSMutableArray *photoURLsLargeImage; // URL to larger image
NSMutableData *receivedData;
NSURLConnection *theConnection;
NSURLRequest *request;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet RootViewController *rootViewController;
#property (nonatomic, readonly) RMMapContents *mapContents;
#end
Inside a function I see this line:
- (void)foo:(xyz *)abc{
..
RMMapContents *mapContents = [self mapContents];
..
}
So, taking it from C++, the mapContents seem like it is not a global scope var (after all, that's why they call them properties, right?), but isn't defining the same name again inside the function weird a bit?
I hope someone can clarify a little here.
Thanks!
The scope of the #interface block extends upto the #end keyword and is not restricted to the braces {}.
So the #property declaration lies very much inside the scope of the #interface and like cli_hlt rightly answered, it acts like a substitute to setter and getter methods for the mapContents property.
so a property named mapContents, would have setters and getters which look like this :
- (void)setMapContents; //setter
- (RMMapContents *)mapContents; //getter
and would can be accessed from within the class using these methods:
[self setMapContents:newContents];
AND
RMMapContents *contents = [self mapContents];
Well, a property is not just a variable. A property is a variable plus its setter and getter methods. A property is usually said to be backed by a variable, which usually(but not always) has the same name as the property itself.
So there are basically three scenarios:
The developer has redefined the backing variable, look for something like:#synthesize mapContents=mapContents_, at the beginning of the implementation -> no problem here.
The compiler defined the variable to be something you don't now but not equal to mapContents - > no problem.
The property backing variable is indeed called "mapContents", so then the local definition hides the global definition (look for a compiler warning here). But by calling [self mapContents] you will not access the global variable but call the getter, which in turn will access the class variable (because then the local mapContents is out of scope)
Hope this helps.
global var mapContents is readonly,in foo function , create a new pointer,then you can change the value of inner var.
Look for a method in your class with a name mapContents that will return a initialization to your RMMapContents class.
Basically this line RMMapContents *mapContents = [self mapContents]; says that initializing an instance of RMMapContents called mapContens using the method mapContents.

why isn't my original variable changed when I pass by reference a CoreData managed Object variable?

why isn't my original variable changed when I pass by reference a CoreData managed Object variable?
So I have in my iPhone application, a coredata managed object called Config. One of the variables in the XCode 4 produced *.h and *.m files is "#dynamic height;" (I point this out because I wonder if it's related to this). When accessing this variable in code it is an NSNumber.
So when I setup a new data selection view/controller I set a variable in this controller to be equal to height, so that when I change it in the next view & come back, it should be changed in the 1st view (pass by ref concept).
Only problem is that it doesn't seem to change the value?
Some code extracts:
#interface Config : NSManagedObject {
#private
}
#property (nonatomic, retain) NSNumber * height;
#end
#implementation Config
#dynamic height;
#end
Calling the 2nd view
// Prepare
SelectorController *sc = [[SelectorController alloc] initWithStyle:UITableViewStyleGrouped];
sc.returnValue = self.config.height;
// Show New Window
[self.navigationController pushViewController:sc animated:YES];
[sc release];
Section Controller
#interface SelectorController : UITableViewController {
NSNumber *_returnValue;
}
#property (nonatomic, retain) NSNumber *returnValue;
#end
NSNumber is immutable, that is, you can't change its value after it's created. When you assign a new value to an NSNumber property, it's a completely different object and the original object doesn't change at all.

Sharing an array between 2 classes

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.

Core Data decorating with sort-compare methods

I have some NSManagedObjects and I would like to write methods for sorting and comparing the properties on them.
My problem is that since Core Data defines the properties as #dynamic they can not be referenced at compile time. This means that decorating an NSManagedObject with methods like this:
- (NSComparisonResult) compareDateAndTime:(Event *) event {
return [originDate compare:[event originDate]];
}
will result in the compiler not being able to locate a property called "originDate".
The above method is called like this:
NSArray *events = [[NSArray alloc]
initWithArray:[unsortedEvents sortedArrayUsingSelector:#selector(compareDateAndTime:)]];
I could go with predicates or fetchedResultController, but I would like to build these as
methods myself as I have an identical NSObjects for each NSManagedObject. This NSObject acts as a temp object that is passed around and populated before it's properties are set on the NSManagedObject that is then persisted. I also have some other functions, like specialized accessors, I would like to add to the NSManagedObject.
**(1)**Is there a general/best practice way of decorating NSManagedObjects with methods and functions **(2)**and have Xcode not overwrite them when "re-building" a class for an Entity?
Thank you for any help or "RTFM" given:)
I re-generate the model classes for my entities fairly often, so I added support methods (including implementations of getters for transient properties) as "Support" categories on the model classes.
I don't ever have to modify the model class files and there is no way I could accidentally overwrite my additional methods.
For example, a "Place" entity might have a name string and latitude/longitude numbers. It also could have a transient property for the first letter of the name. This can be used as the section name key path for section index titles in a large table view.
Xcode will generate the class files for the "Place" entity like this:
Place.h:
#import <CoreData/CoreData.h>
#interface Place : NSManagedObject
{
}
#property (nonatomic, retain) NSString * placeName;
#property (nonatomic, retain) NSNumber * latitude;
#property (nonatomic, retain) NSNumber * longitude;
#end
Place.m:
#import "Place.h"
#implementation Place
#dynamic placeName;
#dynamic latitude;
#dynamic longitude;
#end
I create a "Support" category on the "Place" class.
PlaceSupport.h:
#interface Place (Support)
- (NSString *)uppercaseFirstLetterOfName;
- (CLLocation*)location;
#end
and implement it in PlaceSupport.m
Can you use objectForKey:?
On the other hand, I haven't had a problem with using the properties directly in my code.