0

I have setup push notifications and updates in CK are updating UI well. The issue I have is Syncing between Core Data and CK.

It's set to use NSPersistentCloudKitContainer with NSMergeByPropertyObjectTrumpMergePolicy option.

Its a very simple CD managed object for Player including lastUpdated field - I have private dB with equivalent CD_Player record type and fields paired with the Core Data entities.

- (void)savePlayerToCoreData:(Player *)player {
    NSLog(@"🎯 savePlayerToCoreData");
    
    NSManagedObjectContext *context = self.persistentContainer.viewContext;
    
    [context performBlock:^{
        // 🔍 Fetch the existing player using playerId
        NSFetchRequest *fetchRequest = [Player fetchRequest];
        fetchRequest.predicate = [NSPredicate predicateWithFormat:@"playerId == %@", player.playerId];
        fetchRequest.fetchLimit = 1;
        
        NSError *fetchError = nil;
        NSArray *results = [context executeFetchRequest:fetchRequest error:&fetchError];

        Player *managedPlayer = results.firstObject;

        if (managedPlayer) {
            NSLog(@"🔍 Fetched Player: %@ (playerId: %@, objectID: %@)",
                  managedPlayer.nickName,
                  managedPlayer.playerId,
                  managedPlayer.objectID);
        } else if (!managedPlayer) {
            NSLog(@"❌ Player not found in Core Data. Aborting update to prevent duplicate CloudKit records.");
            return;
        }

        // ✅ Update existing player
        managedPlayer.nickName = player.nickName;
        managedPlayer.avatarHexRgbColor = player.avatarHexRgbColor;
        managedPlayer.gamesWon = player.gamesWon;
        managedPlayer.gamesLost = player.gamesLost;
        managedPlayer.matchesWon = player.matchesWon;
        managedPlayer.matchesLost = player.matchesLost;
        managedPlayer.organizationKey = player.organizationKey;
        managedPlayer.strengths = player.strengths;
        managedPlayer.weaknesses = player.weaknesses;
        managedPlayer.lastUpdated = [NSDate date]; 

        if (player.avatar) {
            managedPlayer.avatar = player.avatar;
        }

        if (![managedPlayer hasChanges]) {
            NSLog(@"⚠️ No changes detected in Core Data. CloudKit sync may not trigger.");
        } else {
            NSLog(@"✅ Changes detected. Saving to trigger sync...");
        }

        NSError *saveError = nil;
        if ([context save:&saveError]) {
            NSLog(@"✅ Core Data context saved. CloudKit Sync should trigger.");
        } 
        NSLog(@"🔍 After Update: %@ (playerId: %@, objectID: %@)", managedPlayer.nickName, managedPlayer.playerId, managedPlayer.objectID);
    }];
}

When the user makes an update on the device, Core Data saves the context and looking at the logs within CK, I see a RecordSave event. My activity in on the device is coming through. All appears fine including the RecordId assignment on the basic logging I have added within my code..

The only issue is - simple test of updating single value for the nickName field is just not updating - it is not reflected in the CK portal view at all.

What could be the reason?

1 Answer 1

0

I managed to resolve this by changing the nsmanagedobject parameter to a standard nsobject.

There must have been an issue with the context I was using.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.