swift - Implement Alamofire into DDD structure -
qustion
i have following structure
class uitableviewcontroller (presentation) -> class contents(domain) -> class api(infrastructure)
contents class gets raw data via api class , forms contents , passes uitableviewcontroller.
i use alamofire networking in api class.
i’ve looked through stackoverflow , found examples uitableviewcontroller directly accesses api class. direct access presentation layer infrastructure layer should not do. how return value alamofire
how achieve implementing alamofire ddd structure?
i want achieve this
uitableviewcontroller
class mytableviewcontroller: uitableviewcontroller { var contents: contents? override func viewdidload() { super.viewdidload() let priority = dispatch_queue_priority_default dispatch_async(dispatch_get_global_queue(priority, 0)) { // task self.contents = contents.get("mycontents") self.tableview.reloaddata() } } }
contents
class contents: nsobject { static func get(contentsname: string) -> contents { let data = myapi.getrequest("https://xxxx.com/mycontents") // form contents let contents = contentsfactory(data) return contents } }
api
class myapi: nsobject { static getrequest(url) -> nsdata { // , return data using alamofire } }
you need deal request being run in background, whereas instance of contents
returned immediately. in completion handler alamofire request, need call contents
instance, call viewcontroller
.
therefore contents
class needs have completion closure passed on get
function, along lines of:
static func get(contentsname: string, completion: (() -> void)?) -> contents {
this closure have passed down api (or through other techniques), can called when alamofire request completes. also, rather have class method background work, better handled in contents
instance create.
then call using:
self.contents = contents.get("mycontents", completion({ dispatch_async(dispatch_get_global_queue(priority, 0)) { self.tableview.reloaddata() } }
Comments
Post a Comment