How to keep a variable in memory until the app qui

2019-04-11 15:17发布

问题:

I have a singleton object in iOS that when instantiated parses a CSV file and then holds the results. I would like to make this object universally accessible and I would like it to not be released from memory until the app quits. I am running ARC so I cannot do manual retains.

Is there a way I can do this so it will work with ARC?

Header File:

#import <Foundation/Foundation.h>
#import "CHCSV.h"
#import "RCParserObject.h"


@interface ParserStore : NSObject <CHCSVParserDelegate>
{
    // CSV Variables
    RCParserObject *item;
    NSMutableArray *data;
    NSMutableArray *parsedData;
    int fields;
    bool open;
}

@property (atomic, retain) RCParserObject *item;
@property (atomic, retain) NSMutableArray *data;
@property (atomic, retain) NSMutableArray *parsedData;
@property (atomic) int fields;
@property (atomic) bool open;

+ (ParserStore *) defaultStore;

- (void) parseCSVFile:(NSString*)file;
- (void) categorizeData;

Implementation File

#import "ParserStore.h"
#import "RCParserObject.h"
#import "RCDetailItem.h"

static ParserStore *defaultStore;

@implementation ParserStore

@synthesize item, data, parsedData, fields, open;

# pragma mark -
# pragma mark Singleton Methods

+ (ParserStore *) defaultStore
{
    if (!defaultStore) 
    {
        defaultStore = [[super allocWithZone:NULL] init];
    }
    return defaultStore;
}

+ (id) allocWithZone:(NSZone *)zone
{
    return [self defaultStore];
}

- (id) init
{
    NSLog(@"Running init on parser store");

    if (defaultStore) 
    {
        NSLog(@"Self data count is %d, right before return", self.parsedData.count);
        return defaultStore;
    }

    // NSLog(@"This better only happen once");
    self = [super init];
    [self setParsedData:[[NSMutableArray alloc] init]];
    [self parseCSVFile:@"ContentNoPathFileExt2ASCII"];
    [self categorizeData];
    // NSLog(@"Self data count is %d when first created", self.parsedData.count);

    return self;
}

    @property (atomic, retain) RCParserObject *item;
    @property (atomic, retain) NSMutableArray *data;
    @property (atomic, retain) NSMutableArray *parsedData;
    @property (atomic) int fields;
    @property (atomic) bool open;

    + (ParserStore *) defaultStore;

    - (void) parseCSVFile:(NSString*)file;
    - (void) categorizeData;

    @end

回答1:

+(MySingleton *)singleton {
    static dispatch_once_t pred;
    static MySingleton *shared = nil;
    dispatch_once(&pred, ^{
        shared = [[MySingleton alloc] init];
        shared.someVar = someValue; // if you want to initialize an ivar
    });
    return shared;
}

From anywhere:

NSLog(@"%@",[MySingleton singleton].someVar);

Note that your iOS app already has a singleton that you can access anywhere:

AppDelegate *delegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];


回答2:

a simple singleton would be something like... in the .h:

@interface Foo : NSObject

+ (Foo *)sharedInstance;

@end

and in the .m:

static Foo *_foo = nil;

@implementation Foo

+ (Foo *)sharedInstance {
  if (!_foo)
    _foo = [[Foo alloc] init];
  return _foo;
}

@end