Search code examples
iosios9semaphorecloudkit

CloudKit method call hung up


When app starts some preliminary process take place. Sometimes it is done quickly in some second, and sometimes It does not end, but without any error it hung up.

I.e. at launch client always fetch the last serverChangedToken. and sometimes it just hung up it does not complete. I am talking about production environment, developer works well. So this route get called, but some times it does not finishes. Any idea why? I do not get any error, timeout.

let fnco = CKFetchNotificationChangesOperation(previousServerChangeToken: nil)
fnco.fetchNotificationChangesCompletionBlock = {newServerChangeToken, error in

    if error == nil {

        serverChangeToken = newServerChangeToken

        dispatch_sync(dispatch_get_main_queue(), {

            (colorCodesInUtility.subviews[10] ).hidden = false
        })

    } else {

        Utility.writeMessageToLog("error 4559: \(error!.localizedDescription)")
    }

    dispatch_semaphore_signal(sema)
}

defaultContainer.addOperation(fnco)
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER)

I know it is not recommended to use semaphores to control flow of the CloudKit method calls.

Do you think the last two line can be swapped? First dispatch_semaphore_wait and then addOperation be called?

Strange that app worked for iOS 8, this bug arise only in iOS 9.


Solution

  • Adding the following line of code will probably solve your problem:

    queryOperation.qualityOfService = .UserInteractive
    

    In iOS 9 Apple changed the behavior of that setting. Especially when using cellular data you could get the behaviour you described.

    The documentation states for the .qualityOfService this: The default value of this property is NSOperationQualityOfServiceBackground and you should leave that value in place whenever possible.

    In my opinion this is more a CloudKit bug than a changed feature. More people have the same issue. I did already report it at https://bugreport.apple.com