Skip to content

Instantly share code, notes, and snippets.

@penso

penso/test.swift Secret

Created Feb 20, 2021
Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@penso 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