我的iOS应用程序中的一个类实现了NSCoding。它在更改文件时将其保存到文件中,并在打开应用程序时从文件中加载。该应用程序很遗憾现在无法启动,如果我在Xcode中暂停执行,无论何时暂停,主线程都会在调用decodeObjectForKey
后尝试执行initWithCoder
。
我知道该文件不是空的,并且在我调试时应用程序不会崩溃。当我从我的设备上直接打开它时会崩溃,很可能是因为启动时间太长了。在此之前,我的设备上的存储空了,但我已经释放了空间。
最好假设保存的对象已损坏?如果是这样,将来我该如何处理?如何识别文件是否已损坏?
NSData *myEncodedObject = [MPSettings dataForKey:@"songCache"];
sharedInstance = (SongManager *)[NSKeyedUnarchiver unarchiveObjectWithData:myEncodedObject]; //<------- Stuck on this
//调用它:
- (id)initWithCoder:(NSCoder *)decoder{
if((self = [super init])){
//decode properties, other class vars
allSongs = [decoder decodeObjectForKey:@"allSongs"];
_groupByAlbum = [decoder decodeObjectForKey:@"groupByAlbum"];
_groupByAlbumx = [decoder decodeObjectForKey:@"groupByAlbumx"];
_groupByArtist = [decoder decodeObjectForKey:@"groupByArtist"];
_groupByArtistx = [decoder decodeObjectForKey:@"groupByArtistx"];
playlists = [decoder decodeObjectForKey:@"playlists"];
_lastUpdated = [decoder decodeObjectForKey:@"lastUpdatedArray"]; //<--- STUCK HERE
if(_lastUpdated == nil){
_lastUpdated = [[NSArray alloc] init];
}
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)encoder{
//Encode properties, other class variables, etc
[encoder encodeObject:[allSongs mutableCopy] forKey:@"allSongs"];
[encoder encodeObject:_groupByAlbum forKey:@"groupByAlbum"];
[encoder encodeObject:_groupByAlbumx forKey:@"groupByAlbumx"];
[encoder encodeObject:_groupByArtist forKey:@"groupByArtist"];
[encoder encodeObject:_groupByArtistx forKey:@"groupByArtistx"];
[encoder encodeObject:playlists forKey:@"playlists"];
[encoder encodeObject:_lastUpdated forKey:@"lastUpdatedArray"];
}
更新
我的手机上的音乐库似乎出现了问题。 NSCoding方法在检索MPMediaItems或歌曲数组的行上停止。重新启动设备后,它再次开始正常运行。