1
votes

I am trying to load, then modify and resave an array.

Here is code, modify func is the top one:

func modifyUserGroupsRequestee(){
    print("step2")
    acceptedUsersArray.append(groupNameLbl.text!)
    //error
    userGroupRecordToUpdate.setObject(acceptedUsersArray as CKRecordValue?, forKey: "userGroups")
    database.save(recordToUpdate) { (savedRecord, error) in
        if error != nil{
            print(error.debugDescription)
        }else{
            print("SAVED RECORD")
        } 
    } 
}

func resaveUserGroups(){
    print(addedUser)
    print("step1")
    // add group to requestees user groups
    let pred = NSPredicate(format: "username = %@", "\(addedUser)")
    let query = CKQuery(recordType: "PersonalUser", predicate: pred)
    let operation = CKQueryOperation(query: query)  
    //operation.resultsLimit = CKQueryOperationMaximumResults
    operation.recordFetchedBlock = { (record: CKRecord!) in
        if record != nil{
            self.userGroupRecordToUpdate = record
           // self.acceptedUsersArray = (record.object(forKey: "userGroups") as! Array)
            print("usergroup names:: \(self.acceptedUsersArray)")
            if let acceptedUserArrays = record.object(forKey: "userGroups") as? [String] {
               // self.feedTableView.reloadData()
                self.acceptedUsersArray = acceptedUserArrays
                print("looks like we r going forward")
                self.modifyUserGroupsRequestee() 
          //  }
            //self.feedTableView.reloadData()
            print(groupNames.count)
            print(self.acceptedUsersArray)
        }   
    }
    database.add(operation)
    //self.tableView.reloadData()
    // print(leaderboardInfo.count)
   }
}

The function prints step1 but never gets to step2. In the bottom function, I have an if let statement I tried to create to solve my nil issue (I commented my previous code above that line- self.acceptedUsersArray... Anyway, I believe I am implementing the if let statement incorrectly, because no data is loaded, even though there is data in cloud kit.

And I do have my personal user cloudKit records set up, here's a pic:

enter image description here

2

2 Answers

0
votes

You should try to keep your code always indented consistently. (In Xcode editor, Cmd+A (Select All), then Ctrl+I (Re-Indent).)

With confusing comments removed, your resaveUserGroups shows as:

func resaveUserGroups() {
    print(addedUser)
    print("step1")
    // add group to requestees user groups
    let pred = NSPredicate(format: "username = %@", "\(addedUser)")

    let query = CKQuery(recordType: "PersonalUser", predicate: pred)

    let operation = CKQueryOperation(query: query)

    operation.recordFetchedBlock = { (record: CKRecord!) in

        if record != nil {
            self.userGroupRecordToUpdate = record
            print("usergroup names:: \(self.acceptedUsersArray)")

            if let acceptedUserArrays = record.object(forKey: "userGroups") as? [String] {
                self.acceptedUsersArray = acceptedUserArrays
                print("looks like we r going forward")
                self.modifyUserGroupsRequestee()

                print(groupNames.count)
                print(self.acceptedUsersArray)
            }
        }
        database.add(operation)
    }
}

Omitting some parts to clarify:

func resaveUserGroups() {
    //...

    operation.recordFetchedBlock = { (record: CKRecord!) in

        if record != nil {
            //...
        }
        database.add(operation)
    }
}

The line database.add(operation) exists inside the recordFetchedBlock.

You may need to fix some more parts (that's another story), but at least, you need to move the line out of the closure to execute the operation you have created:

func resaveUserGroups() {
    //...

    operation.recordFetchedBlock = { (record: CKRecord!) in

        if record != nil {
            //...
        }
        //database.add(operation) 
    }   //↓
    database.add(operation)
}
0
votes

I just solved it. Apparently there always needs to be some kind of value inside the array even if it was just created. I was trying to query an array that existed in the recordType, but not yet under the specific record.