ios - Two classes, callback and unit testing -


i have 1 class static methods : class wraps calls twitter api

in second class, have business logic.

due asynchronousness behaviour of methods in wrapper class, have difficulties design communication. here i've done :

apimanager.swift

public class apimanager {     class func getpermission(callback : () -> void) {          let accountstore = acaccountstore()         let accounttype =          acaccountstore().accounttypewithaccounttypeidentifier(acaccounttypeidentifiertwitter)          let callbackrequestaccess = { (granted: bool, error: nserror!) -> void in             ...             if(granted) {                 callback()             }          }          accountstore.requestaccesstoaccountswithtype(setaccounttype,                       options: nil, completion: callbackrequestaccess)      } } 

welcome.swift

public class welcome {      public func checkpermission() {         apimanager.getpermission(gettweet)     }      public func gettweet() {         ...     }         } 

i not sure design in right or not. don't want have strong link between classes, that's why using callback.

is classic design ? moreover, don't feel behaviour easy test ?

you improve testability not using class methods here. create twitterconnection protocol. create systemtwitterconnection conforms , manages things through acaccountstore. create testtwitterconnection returns pre-set responses can configure testing. create keychaintwitterconnection manage twitter logins hand without using acaccountstore, or other implementation if apple comes out yet way store these accounts.

pass appropriate connection welcome when created.

if twitterconnection protocol gets large, should consider splitting smaller protocols, such twitterauthenticator , tweetfetcher handle fewer things (even if single type implements of protocols). can make testing easier allowing test types implement few functions rather dozens.

the use of closures fine, should stick more closely cocoa naming conventions. you're calling callback traditionally called completion. i'd follow cocoa's lead on how name methods. rather getpermission(), requestaccesswithcompletionhandler(). caller understand has similar behavior requestaccesstoaccountswithtype(options:completion:). don't built new vocabulary caller.


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

javascript - Passport with Express 4 -