Reputation: 1650
I'm trying to add iCloud syncing to my game, by storing save data filename as a key, and the file in a CKAsset
. However, the program always crashes when it tries to setObject: forKey:
on the CKRecord
, even if I use a simple dummy string instead of the file asset.
If I don't assign any objects to the CKRecord
, the record successfully saves to iCloud, as I can see the record with the correct name in the iCloud Dashboard.
Nothing is logged to the debugger output when the exception is thrown. What could cause this crash?
bool iCloudSync::UploadToCloud( SaveDataManager::CloudOperationComplete Callback, void *UserData, std::vector< const char * > *PathsToUpload )
{
m_UploadCallback = Callback;
m_UploadCallbackUserData = UserData;
if (PathsToUpload == NULL) return false;
// TODO: [[CKContainer defaultContainer] accountStatusWithCompletionHandler] to check if there is a signed-in icloud user
CKDatabase *db = [[CKContainer defaultContainer] privateCloudDatabase];
CKRecord *record = [[CKRecord alloc] initWithRecordType:@"SaveDataFiles"];
for (int i = 0; i < PathsToUpload->size(); i++) {
NSString *filenameString = [NSString stringWithCString:(*PathsToUpload)[i] encoding:NSASCIIStringEncoding];
NSURL *documentsURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
NSURL *fullPathURL = [documentsURL URLByAppendingPathComponent:filenameString];
NSError *error = nil;
bool reachable = [fullPathURL checkResourceIsReachableAndReturnError:&error];
if (reachable) {
CKAsset *fileAsset = [[CKAsset alloc] initWithFileURL:fullPathURL];
// CRASH HERE Trying to assign any value to a key
// record[filenameString] = fileAsset;
[record setObject:@"Dummy test string" forKey:filenameString];
}
}
// TODO: completion handler
// TODO: always overwrite remote
[db saveRecord:record completionHandler:nil];
// CKModifyRecordsOperation *modify = [[CKModifyRecordsOperation alloc] initWithRecordsToSave:[NSArray arrayWithObject:record] recordIDsToDelete:nil];
// Success!
return true;
}
Upvotes: 1
Views: 393
Reputation: 1650
The error that was logged was:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'recordKey (gamestate1.dat) contains invalid characters'
The reason nothing was being logged was because my exception breakpoint was getting triggered several times in a row before the logging actually happened. I just needed to keep pressing continue before it logged the exception output.
So, I just need to find some other key I can use that doesn't include a dot, which is frustratingly an invalid character.
For reference, the valid characters are that the first character must be alphabetical, subsequent characters are alphanumeric or underscores. Here is Apple's phrasing:
Key names consist of one or more alphanumeric characters and start with a letter. You may also include underscore characters if you do not use an underscore as the first character in the name. Spaces are not allowed in key names. The names of your keys must not match the names of any properties of this class.
Upvotes: 1