iphone - Internal memory in iOS simulator -
i finished second tutorial ios development. when add new sighting, works fine , turns in list of sightings. however, when close ios simulator , reopen it, first entry there (it added manually). should there internal memory in ios simulator after closed , reopened? there seems memory if add contact info contacts, still there when reopen it. if so, how make sure array in datacontroller file stored on simulator/phone doesn't clear every time reopen simulator?
thanks
you need use persistent storage if want save data between sessions. options include:
you can use plist files. example if have
nsarray *array
, can save plist usingwritetofile
:[array writetofile:filename atomically:yes];
you can read array with:
nsarray *array = [nsarray arraywithcontentsoffile:filename];
this technique works standard
nsstring
,nsnumber
, etc., objects, not custom objectsbirdsighting
, though.for custom objects
birdsighting
, usenskeyedarchiver
,nskeyedunarchiver
. way, these not useful classes saving data small data sets this, given features prominently in new ios 6 state preservation features, it's worth familiarizing pattern.you can use
nsuserdefaults
. better suited app settings , defaults, theoretically used saving data, too.you can use coredata. preferred ios technology object persistence. it's powerful , engineered framework (though tad complicated) , suited if you're dealing more significant amounts of data.
you can use sqlite, too. see ray wenderlich article on using sqlite. , once start using sqlite, can consider using fmdb simplify coding effort.
if wanted, example, use second approach, nskeyedarchiver
, nskeyedunarchiver
, first thing might want make birdsighting
conform nscoding
, altering @interface
declaration in birdsighting.h
say:
@interface birdsighting : nsobject <nscoding>
second, have write 2 nscoding
methods, initwithcoder
, encodewithcoder
, in birdsighting.m
define properties can loaded/saved object:
- (nsarray *)keysforencoding; { return @[@"name", @"location", @"date"]; } - (id) initwithcoder:(nscoder *)adecoder { self = [super init]; if (self) { (nsstring *key in [self keysforencoding]) { [self setvalue:[adecoder decodeobjectforkey:key] forkey:key]; } } return self; } - (void)encodewithcoder:(nscoder *)acoder { (nsstring *key in self.keysforencoding) { [acoder encodeobject:[self valueforkey:key] forkey:key]; } }
your birdsighting
can loaded , saved nskeyedunarchiver
, nskeyedarchiver
, respectively.
so, focusing on loading of sightings, have (a) tell birdsightingdatacontroller.m
file for; , (b) instruct read file during initialization:
- (nsstring *)filename { nsstring *docspath = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes)[0]; return [docspath stringbyappendingpathcomponent:@"birdsightings"]; } - (void)initializedefaultdatalist { nsstring *filename = [self filename]; self.masterbirdsightinglist = nil; if ([[nsfilemanager defaultmanager] fileexistsatpath:filename]) { self.masterbirdsightinglist = [nskeyedunarchiver unarchiveobjectwithfile:filename]; } if (!self.masterbirdsightinglist) { nsmutablearray *sightinglist = [[nsmutablearray alloc] init]; self.masterbirdsightinglist = sightinglist; birdsighting *sighting; nsdate *today = [nsdate date]; sighting = [[birdsighting alloc] initwithname:@"pigeon" location:@"everywhere" date:today]; [self addbirdsightingwithsighting:sighting]; } }
the birdsightingdatacontroller.m
can define method save data:
- (bool)save { return [nskeyedarchiver archiverootobject:self.masterbirdsightinglist tofile:[self filename]]; }
you can now, example, call save
method whenever add sighting, e.g.:
- (void)addbirdsightingwithsighting:(birdsighting *)sighting { [self.masterbirdsightinglist addobject:sighting]; [self save]; }
personally, rather saving every time user change in app, might rather have app delegate save when app goes background or terminates (but requires further changes, won't go now).
but code illustrates how can use nskeyarchiver
, nskeyunarchiver
save , load data. , clearly, more complicated scenarios, encourage consider core data. small data sets, this, archiver pattern can useful (and said, worth being familiar because basic technology used in ios 6 app state restoration).
Comments
Post a Comment