Silver Challenge--Console says mappoint saved but it isn't


#1

I"ve deployed whereami to an iPhone 5 with iOS 6 and when I hit the home button, I get the message to my console that “Saved all of the BNRMapPointItems” as should be. However, when I close out XCode and reopen the app, the mappoint is gone. So it seems as if the data isn’t being committed to the file, but I’m having a hard time figuring out why this is occurring. In BNRMapPoint.m, I’ve added the encoder and decoder methods:

[code]- (void)encodeWithCoder:(NSCoder *)aCoder
{
double coordLatitude = coordinate.latitude;
double coordLongitude = coordinate.longitude;

[aCoder encodeDouble:coordLatitude forKey:@"coordLatitude"];
[aCoder encodeDouble:coordLongitude forKey:@"coordLongitude"];
[aCoder encodeObject:title forKey:@"title"];

}

  • (id)initWithCoder:(NSCoder *)aDecoder
    {
    self = [super init];
    if (self) {
    double coordLatitude = [aDecoder decodeDoubleForKey:@“coordLatitude”];
    double coordLongitude = [aDecoder decodeDoubleForKey:@“coordLongitude”];

      [self setTitle:[aDecoder decodeObjectForKey:@"title"]];
      
      coordinate = CLLocationCoordinate2DMake(coordLatitude, coordLongitude);
    

    }
    return self;
    }
    [/code]

Then I created a store object and added a pointer to a BNRMapPoint object to have a reference to it. In BNRMapPointStore.h I have:

[code]#import <Foundation/Foundation.h>

#import “BNRMapPoint.h”

@interface BNRMapPointStore : NSObject

@property (nonatomic, strong) BNRMapPoint *mapPoint;

  • (BNRMapPointStore *)sharedStore;
  • (NSString *)itemArchivePath;
  • (BOOL)saveChanges;

@end[/code]

And in BNRMapPointStore.m I added the archive path and saveChanges methods:

[code]#import “BNRMapPointStore.h”

@implementation BNRMapPointStore

@synthesize mapPoint;

  • (BNRMapPointStore *)sharedStore
    {
    static BNRMapPointStore *sharedStore = nil;
    if (!sharedStore)
    sharedStore = [[super allocWithZone:nil] init];

    return sharedStore;
    }

  • (id)allocWithZone:(NSZone *)zone
    {
    return [self sharedStore];
    }

  • (id)init
    {
    self = [super init];
    if (self) {

      NSString *path = [self itemArchivePath];
      mapPoint = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    

    }

    return self;
    }

  • (NSString *)itemArchivePath
    {
    NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    // Get one and only document from that list
    NSString *documentDirectory = [documentDirectories objectAtIndex:0];
    return [documentDirectory stringByAppendingPathComponent:@“items.archive”];
    }

  • (BOOL)saveChanges
    {
    // returns success or failure
    NSString *path = [self itemArchivePath];

    return [NSKeyedArchiver archiveRootObject: mapPoint toFile:path];
    }
    @end[/code]

And last, I added code in whereAmIAppDelegate.m to commit data to the file when the app closes:

[code]- (void)applicationDidEnterBackground:(UIApplication *)application
{
BOOL success = [[BNRMapPointStore sharedStore] saveChanges];

if (success) {
    NSLog(@"Saved all of the BNRMapPointItems");
}
else {
    NSLog(@"Could not save any of the BNRMapPointItems");
}

}[/code]


#2

Okay, the problem is that you need to repopulate your map with the archived mappoints. so when you load the viewcontroller check if there is a saved archive and if not then loop through them and add to worldview.

post your WhereamiViewController.m file if you want me too help you further