views:

132

answers:

2

I'm trying to save an NSMutableArray called queueArray so it can be loaded again after the app has been quit. I used a few tutorials to get me going and this is the code I have come up with. The problem seems to be that "initWithCoder" and "encodeWithCoder" are not being called, shown by no NSLog calls and no stopping at breakpoints. I have added the NSCoding protocol to the .h file and I know that queueArray is not nil and it contains MPMediaItems. Here is some of the code I use to try to save and load the array:

-(IBAction)saveQueuePressed {

     NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
     NSString *filePath = [rootPath stringByAppendingPathComponent:@"queueArray.archive"];

     //should cause encodeWithCoder to be called
     [NSKeyedArchiver archiveRootObject:queueArray toFile:filePath];

}

-(IBAction)loadQueuePressed {

     NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
     NSString *filePath = [rootPath stringByAppendingPathComponent:@"queueArray.archive"];

     //should cause initWithCoder to be called
     queueArray = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];

}

-(void)encodeWithCoder:(NSCoder *)coder {

     NSLog(@"encodeWithCoder");

     [coder encodeObject:queueArray forKey:@"savedQueueArray"];
}



-(id)initWithCoder:(NSCoder *)decoder {

     NSLog(@"initWithCoder");

     queueArray = [decoder decodeObjectForKey:@"savedQueueArray"];

     return self;

}

Any help will be greatly appreciated!

A: 

The encodeWithCoder: and initWithCoder methods are called when you archive/unarchive an object that responds to them. From what I understand, you have those methods in your class, but the object you are actually archiving (queueArray) is not an instance of that class, it's an NSMutableArray.

If you do want to save your entire object, you can change your saveQueue method to this

-(IBAction)saveQueuePressed {

    NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *filePath = [rootPath stringByAppendingPathComponent:@"queueArray.archive"];

    // saving the array shouldn't, but saving the self object
    //should cause encodeWithCoder to be called:
    [NSKeyedArchiver archiveRootObject:self toFile:filePath];
}

But if you just want to save the array, I guess you can just use saveQueuePressed and loadQueuePressed, I don't think you need the encode/init WithCoder: methods

Update: Maybe your path is not right. Try

NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [[rootPath stringByAppendingPathComponent:@"queueArray.archive"] stringByExpandingTildeInPath]];
filipe
I have changed the code to: //to saveNSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];NSString *filePath = [NSString stringWithFormat:@"%@/queueArrayFile", rootPath]; [queueArray writeToFile:filePath atomically:NO]; //to retrieveNSMutableArray *retrievedArray = [[NSMutableArray alloc] initWithContentsOfFile:filePath]; However, it is still not working as the array is empty when I retrieve it. I dont think I am able to save an MSMutableArray full of MPMediaItems. What should I do?
Craig
maybe the path is not good?
filipe
A: 

Response to imaginaryboy's answer:

I have tried changing the path code but it has not fixed the problem. Apparently MPMediaItem does implement the NSCoding protocol but has a pointer to MPMediaItemArtwork which doesn't, which is causing the problems. Could someone please give me some guidance on what to do (My knowledge is very limited in this field)?

sources: http://stackoverflow.com/questions/2444870/mpmediaitem-nscoding-problem-with-mpmediaitemartwork

Thanks for any input!

Craig