Home Swift 4 accessing completionhandler returned value from class variable
Reply: 0

Swift 4 accessing completionhandler returned value from class variable

user1374
1#
user1374 Published in May 21, 2018, 6:36 pm

Problem: I’m trying to access the value which is returned from completionHandler, to assign it to a variable which is outside the scope of completionHandler returned method. I can access the variable in the scope of the method, but I can’t from outside. I’ve tried to assign it to the class variable when I access, but didn’t work. Any ideas?

var marketler = [MarketModel]()
var marketAdiArray = [String]()

override func viewDidLoad() {
    getMarkets { (marketdizi) in
        self.objAryToTableView(markets: marketdizi)
        print(self.marketAdiArray) // -> this returns the right array
    }

    print(self.marketAdiArray) // -> this returns an empty array
 }

func getMarkets(completionHandler : @escaping ([MarketModel])->()) {
    let uid = "userID(02)"
    print("uid : \(uid)")
    MobileUserViewModel().getUser(userId: uid, completionHandler: { (user) in
// here returns an user object
        self.loginUser = user
        MarketViewModel().getMarketFromDb(mobilUser: user, completionHandler: { (marketler) in
            print("marketler : \(marketler)")
            completionHandler(marketler)
        })
    })
}

 func objAryToTableView(markets : [MarketModel]) {
    var ary = [String]()
    for m in markets {
        ary.append(m.marketName as String!)
    }
    self.marketAdiArray = ary
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.315647 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO