Search code examples
iosswiftclosurespromisekit

PromiseKit ios Swift closure return type issue


I am using Promise Kit in my app. I am new to promise kit. I need to know actually how this closure works. So if anyone can explain the thing with example that would be very helpful.

Problem i am facing is, though i am returning the expected value, i am getting an error stating Cannot convert return expression of type....

I think maybe i understood the whole thing wrong here.

This is how i am using promise chains.

Glyph().glyphs(book: self.downloadingBook!._id)
                        .then { glyphs -> Promise<[Glyph]> in
                            self.setProgress(progress: 0.07)
                            return Glyph().saveGlyphs(data: glyphs)
                        }.then { lines -> Promise<[Line]> in
                            self.setProgress(progress: 0.14)
                            return Line().lines(book: self.downloadingBook._id)
                        }.then { lines -> Promise<[Line]> in
                            self.setProgress(progress: 0.21)
                            return Line().saveLines(data: lines)
                        }.then { surahs -> Promise<[SurahGlyph]> in
                            self.setProgress(progress: 0.28)
                            return SurahGlyph().surahs(book: self.downloadingBook._id)
                        }.then { surahs -> Promise<[SurahGlyph]> in
                            self.setProgress(progress: 0.35)
                            return SurahGlyph().saveSurahGlyphs(data: surahs)
                        }.then { surahs -> Promise<[SurahGlyph]> in
                            self.setProgress(progress: 0.42)
                            return Translator().translators()
                        }.then { data -> Promise<[Translator]> in
                            self.setProgress(progress: 0.49)
                            return Translator().saveTranslators(translators: data)
                        }.then { data -> Promise<[Translator]> in
                            self.setProgress(progress: 0.56)
                            return Surah().getSurahs(translatorId: "ar.tazweed")
                        }.then { surahs -> Promise<[Surah]> in
                            self.setProgress(progress: 0.63)
                            return Surah().saveSurahs(surahs: surahs)
                        }.then { surahs -> Promise<[Surah]> in
                            self.setProgress(progress: 0.70)
                            Ayah().ayahs(page: 1)
                        }.then { ayahs -> Promise<[Ayah]> in
                            self.setProgress(progress: 0.77)
                            Ayah().saveAyahs(ayahs: ayahs)
                            return Promise.value(ayahs)
                        }.done { _ in
                            self.setProgress(progress: 1)
                            self.statusLabel.text = "Completed"
                            self.reloadBook()
                        }.catch { error in
                            print(error)
                    }

This is how i am returning from my functions:

func translators() -> Promise<[Translator]> {
        do {
            let realm = try Realm()
            let translatorsInRealm = realm.objects(Translator.self)
            if translatorsInRealm.count != 0 {
                Promise.value(Array(translatorsInRealm))
            } else {
                return Promise<[Translator]> { seal in
                    let handler = RequestHandler()
                    let session = RequestHandler.alamofireManager
                    session.request(API.Quran.translatorsURL, method: .get, parameters: nil,
                                    encoding: URLEncoding.default, headers: nil,
                                    interceptor: handler).validate().response { (response) in
                                        if response.result.isSuccess {
                                            do {
                                                let linesFromServer = try JSONDecoder().decode([Translator].self, from: response.data!)
                                                seal.fulfill(linesFromServer)
                                            } catch {
                                                seal.reject(error)
                                            }
                                        } else {
                                            seal.reject(response.error!)
                                        }
                    }
                }
            }
        } catch {
            print("Unexpected error: \(error).")
        }

        return Promise.value([])
    }

Errors i am getting

Errors i am getting


Solution

  • There are mismatch in the promise value types. The corrected chain will look like this,

    Glyph().glyphs(book: self.downloadingBook!._id)
                            .then { glyphs -> Promise<[Glyph]> in
                                self.setProgress(progress: 0.07)
                                return Glyph().saveGlyphs(data: glyphs)
                            }.then { lines -> Promise<[Line]> in
                                self.setProgress(progress: 0.14)
                                return Line().lines(book: self.downloadingBook._id)
                            }.then { lines -> Promise<[Line]> in
                                self.setProgress(progress: 0.21)
                                return Line().saveLines(data: lines)
                            }.then { surahs -> Promise<[SurahGlyph]> in
                                self.setProgress(progress: 0.28)
                                return SurahGlyph().surahs(book: self.downloadingBook._id)
                            }.then { surahs -> Promise<[SurahGlyph]> in
                                self.setProgress(progress: 0.35)
                                return SurahGlyph().saveSurahGlyphs(data: surahs)
                            }.then { surahs -> Promise<[Translator]> in
                                self.setProgress(progress: 0.42)
                                return Translator().translators()
                            }.then { data -> Promise<[Translator]> in
                                self.setProgress(progress: 0.49)
                                return Translator().saveTranslators(translators: data)
                            }.then { data -> Promise<[Surah]> in
                                self.setProgress(progress: 0.56)
                                return Surah().getSurahs(translatorId: "ar.tazweed")
                            }.then { surahs -> Promise<[Surah]> in
                                self.setProgress(progress: 0.63)
                                return Surah().saveSurahs(surahs: surahs)
                            }.then { surahs -> Promise<[Ayah]> in
                                self.setProgress(progress: 0.70)
                                return Ayah().ayahs(page: 1)
                            }.then { ayahs -> Promise<[Ayah]> in
                                self.setProgress(progress: 0.77)
                                Ayah().saveAyahs(ayahs: ayahs)
                                return Promise.value(ayahs)
                            }.done { _ in
                                self.setProgress(progress: 1)
                                self.statusLabel.text = "Completed"
                                self.reloadBook()
                            }.catch { error in
                                print(error)
                        }