jeudi 19 janvier 2017

Testing the error routine in an iCloudKit based app

Swift 3.0 iOS 10.x

Wrote this code to delete a record in iCloud; it works, but I want to test some of the error branches? How can I get iCloud to generate errors? Indeed does this code make sense?

 func files_zapTag(theUUID:String) {
    print("files_zapTag \(theUUID)")
    self.theApp.isNetworkActivityIndicatorVisible = true
    self.privateDB.delete(withRecordID: CKRecordID(recordName: theUUID), completionHandler: {recordID, error in
        self.theApp.isNetworkActivityIndicatorVisible = false

        guard error == nil else {
            if let ckerror = error as? CKError {

                if ckerror.code == CKError.requestRateLimited {
                    let retryInterval = ckerror.userInfo[CKErrorRetryAfterKey] as? TimeInterval
                    DispatchQueue.main.async {
                        let userdict = ["theUUID": theUUID]
                        Timer.scheduledTimer(timeInterval: retryInterval!, target: self, selector: #selector(self.files_zapTagR), userInfo: userdict, repeats: false)
                    }
                } else if ckerror.code == CKError.zoneBusy {
                    let retryInterval = ckerror.userInfo[CKErrorRetryAfterKey] as? TimeInterval
                    DispatchQueue.main.async {
                        let userdict = ["theUUID": theUUID]
                        Timer.scheduledTimer(timeInterval: retryInterval!, target: self, selector: #selector(self.files_zapTagR), userInfo: userdict, repeats: false)
                    }
                } else if ckerror.code == CKError.limitExceeded {
                    let retryInterval = ckerror.userInfo[CKErrorRetryAfterKey] as? TimeInterval
                    DispatchQueue.main.async {
                        let userdict = ["theUUID": theUUID]
                        Timer.scheduledTimer(timeInterval: retryInterval!, target: self, selector: #selector(self.files_zapTagR), userInfo: userdict, repeats: false)
                    }
                } else if ckerror.code == CKError.notAuthenticated {
                    NotificationCenter.default.post(name: Notification.Name("noCloud"), object: nil, userInfo: nil)
                } else if ckerror.code == CKError.networkFailure {
                    NotificationCenter.default.post(name: Notification.Name("networkFailure"), object: nil, userInfo: nil)
                } else if ckerror.code == CKError.networkUnavailable {
                    NotificationCenter.default.post(name: Notification.Name("noWiFi"), object: nil, userInfo: nil)
                } else if ckerror.code == CKError.quotaExceeded {
                    NotificationCenter.default.post(name: Notification.Name("quotaExceeded"), object: nil, userInfo: nil)
                } else if ckerror.code == CKError.partialFailure {
                    NotificationCenter.default.post(name: Notification.Name("partialFailure"), object: nil, userInfo: nil)
                } else if (ckerror.code == CKError.internalError || ckerror.code == CKError.serviceUnavailable) {
                    NotificationCenter.default.post(name: Notification.Name("serviceUnavailable"), object: nil, userInfo: nil)
                }
            } // end of guard statement
            return
        }
    })
}

func files_zapTagR(sender: Timer) {
    if let userInfo = sender.userInfo as! NSDictionary? as? [String: Any] {
        let theUUID2Zap = userInfo["theUUID"]! as! String
        files_zapTag(theUUID: theUUID2Zap)
    }
}

Aucun commentaire:

Enregistrer un commentaire