1
votes

Class name MyData has 75+ properties that are needed in throughout 7 Scenes.

Currently, I pass the the instance of MyData file with the code below:

in SceneOne:

MyData *myData = [[MyData alloc]init];

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    UIViewController *destinationViewController = segue.destinationViewController;
    SceneTwo *sceneTwo = [destinationViewController isKindOfClass:[SceneTwo class]] ? (SceneTwo*)destinationViewController : nil;
    sceneTwo.myData = self.myData;
}

This allows me to access any properties as myData.anyProperty

When the 7th Scene is dismissed, I set myData = NULL, and the app returns to SceneOne and a new instance of MyData is initialized.

I'm trying to accomplish above via sharedInstance.

MyData.h

#import <foundation/Foundation.h>

@interface MyData : NSObject {

@property (nonatomic, retain) NSString *someProperty;
// 74 other properties

+ (id)sharedData;

@end

MyData.m

#import "MyData.h"

@implementation MyData

@synthesize someProperty;

+ (id)sharedData {
    static Mydata *sharedData = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedData = [[self alloc] init];
    });
    return sharedData;
}

@end

Question 1: Will the code above work to access the same instance with the code below in any of the Scenes:

 MyData *myData = [MyData sharedData];

Question 2: Can I still access the files as myData.anyProperty ?

Question 3: How do I trigger a new instance of MyData and delete the current instance?

Question 4: I'm downloading a myData file off the web that's a duplicate of MyData class but the properties contain data, and I want Singelton to provide a new instance with the data from the downloaded file, what would be the code for that? i.e myData = [newDownloadedFile copy];

Question 5: Is there an advantage of using Singleton Method Vs. my current method?

1

1 Answers

2
votes
  1. Yes
  2. Yes
  3. You don't. Why do you believe this is necessary? Can you instead add a reset method to MyData?
  4. You shouldn't mix the singleton pattern with a multiple-instance usage case. If you truly want a singleton, think about adding an additional layer to your data set. For example, you may have a local data and remote data configuration. If this is what you want, you may have to change the interface (header) of MyData to make this possible.
  5. A singleton is a single instance of a class across a process. When you want to access the same collection of data from multiple locations in your code, a singleton object is one way you can accomplish this. Otherwise you need to instantiate an object and pass its address to all interested classes so they are each accessing the same instance. That's an oversimplification but I believe it addresses your question.

Regarding your comment for number 3, if you have a singleton, you don't want to reset the data for the entire app if you simply don't need the data in one place anymore. So consider the impact that would have. If you no longer need the MyData object, simply don't use it anymore. Singleton objects typically persist for the lifetime of an app, so it is not common to release/dispose of the object.

For number 4, say you currently have a property called player with a method declaration like this:

- (Player *)currentPlayer;

If you have multiple configurations available, you would add a parameter to your method interface and implementation like this:

- (Player *)currentPlayerForConfiguration:(NSInteger)configuration;

You could decide to use a number, string, or something else to differentiate between different configurations of your data. If you use a number, 0 could be local, 1 could be remote, 2 could be either (for example, check local data first, and if not there, then check remote). If you only have two options, you could use a BOOL and define your method like this:

- (Player *)currentPlayerUsingLocalData:(BOOL)useLocalData;