0

I have a NSMutableOrderedSet property named currentSongsList on a NSObject subclass. I have a NSData property named currentSongsList on a NSManagedObject subclass.

I want to archive the NSMutableOrderedSet property into the database, so that it can be retrieved after my app is terminated and restarted.

In CurrentSongsInfo.h -

@interface CurrentSongsInfo : NSObject

@property (nonatomic, strong) NSMutableOrderedSet *currentSongsList;

In CurrentSongsInfoArchive.h -

@interface CurrentSongsInfoArchive : NSManagedObject

@property (nonatomic, retain) NSData * currentSongsList;

In CurrentSongsInfoArchive.m -

DatabaseInterface *databaseInterfacePtr = [[DatabaseInterface alloc] init];
// Not showing code for databaseInterfacePtr:newManagedObjectOfType, but it has been working for over a year for many entity types
CurrentSongsInfoArchive *currentSongsInfoArchive = (CurrentSongsInfoArchive *)[databaseInterfacePtr newManagedObjectOfType:@"CurrentSongsInfoArchive"];

NSData *currentSongsList = [NSKeyedArchiver archivedDataWithRootObject:currentSongsInfoArchive.currentSongsList];
currentSongsInfoArchive.currentSongsList = currentSongsList;

When the above code from CurrentSongsInfoArchive.m runs, I see the following output:

currentSongsList.count = 540

2014-09-19 04:30:57.219 MusicByCarlCoreData[2863:1091413] currentSongsInfoArchive.currentSongsList.length = 135

I have four other NSMutableOrderedSet properties, whose count values are all different. But when they are archived into the CurrentSongsInfoArchive object, their length is always 135.

NSMutableOrderedSet conforms to NSSecureCoding, but is it even possible to save a NSMutableOrderedSet to an attribute in a NSManagedObject subclass?

Carl Smith
  • 1,236
  • 15
  • 18
  • hmm, are you holding the songs also in the DB? If you are not enctrypting the list, you could just create a normal relationships between Song and PlayList and hold this as "orderedSet" – geo Sep 19 '14 at 12:27
  • The songs are in the DB, but the lists in the CurrentSongsInfo object hold only the songInternalIDs as NSNumbers. When I tried to hold the lists in the database, the performance was miserable. The currentSongsList can contain the songInternalIDs for every song in the user's iTunes library. I'm trying to find a different approach that doesn't involve storing the entire list, but still allows the NowPlayingViewController to potentially choose from every song in the database. So far, I haven't figured out a way to do that. – Carl Smith Sep 19 '14 at 14:00
  • Have you tried to store those NSMutableOrderedSets as an NSMutableArray? I've managed to store arrays into NSData attributes and load them back. http://stackoverflow.com/questions/25416132/how-do-you-store-data-from-nsmutable-array-in-core-data/25417855#25417855 – mitrenegade Sep 20 '14 at 03:04
  • Thanks, mitrenegade, I'll give that a try! – Carl Smith Sep 20 '14 at 16:22

1 Answers1

0

Thanks, @geo and @mitrenegade, for your suggestions. Sadly, it was a silly bug in my archiving code that caused the problem. Since the archiving was bad, there was nothing to unarchive.

So, the final answer... Yes it is possible to save a NSMutableOrderedSet to a NSData attribute in a NSManagedObject subclass.

Carl Smith
  • 1,236
  • 15
  • 18