Skip to content

Instantly share code, notes, and snippets.

@penso

penso/test.swift Secret

Created February 20, 2021 10:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save penso/9c695279e4101fed5dc74c969d5399bf to your computer and use it in GitHub Desktop.
Save penso/9c695279e4101fed5dc74c969d5399bf to your computer and use it in GitHub Desktop.
func refreshDocument(_ documentStruct: DocumentStruct) -> Promises.Promise<Bool> {
self.documentRequest.fetchDocumentUpdatedAt(documentStruct.uuidString)
.then(on: self.backgroundQueue) { documentType in
guard let updatedAt = documentType.updatedAt, updatedAt > documentStruct.updatedAt else {
return Promise(false)
}
return self.documentRequest.fetchDocument(documentStruct.uuidString)
.then { documentType in
try self.saveRefreshDocument(documentStruct, documentType)
return Promise(true)
}
}.catch(on: self.backgroundQueue) { error in
if case APIRequestError.notFound = error {
try? self.deleteLocalDocumentAndWait(documentStruct)
}
}
}
@penso
Copy link
Author

penso commented Feb 22, 2021

Better:

    func refreshDocument(_ documentStruct: DocumentStruct) -> Promises.Promise<Bool> {
        self.documentRequest.fetchDocumentUpdatedAt(documentStruct.uuidString)
            .then(on: self.backgroundQueue) { documentType -> Promises.Promise<DocumentAPIType> in
                if let updatedAt = documentType.updatedAt, updatedAt > documentStruct.updatedAt {
                    return self.documentRequest.fetchDocument(documentStruct.uuidString)
                }
                return Promise(documentType)
            }.then(on: self.backgroundQueue) { documentType in
                if let updatedAt = documentType.updatedAt, updatedAt > documentStruct.updatedAt {
                    try self.saveRefreshDocument(documentStruct, documentType)
                }
            }.catch(on: self.backgroundQueue) { error in
                if case APIRequestError.notFound = error {
                    try? self.deleteLocalDocumentAndWait(documentStruct)
                }
            }.then(on: self.backgroundQueue) { documentType in
                return Promise((documentType.updatedAt ?? documentStruct.updatedAt) > documentStruct.updatedAt)
            }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment