iOS Modle 框架

1.建立 DataSource : NSObject 文件 這個文件是數(shù)據(jù)最底層, 是對AFN 等請求庫的封裝和后臺接口的解析(在DataSource中Block回調的數(shù)據(jù)是最外層的)。

DataSource.h?

+ (DataSource *)sharedInstance;

- (void)bulletinListFromOffset:(NSInteger)offset success:(void (^)(NSDictionary *))success failure:(void (^)(NSInteger))failure;

DataSource.m

#pragma mark - Singleton -

+ (DataSource *)sharedInstance {

? ? ? ? ? ?@synchronized(self) {

? ? ? ? ? ? ? ? ? ? if (s_sharedInstance == nil) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? s_sharedInstance = [[self alloc] init];

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? }

? ? ?return s_sharedInstance;

}

#pragma mark - Init -

- (id)init {

? ? ? ? ? ? self = [super init];

? ? ? ? ? ? if (self) {

? ? ? ? ? ? ? ? ? m_netClient = [[NetClient alloc] initWithBaseURL:[NSURL URLWithString:SERVER_BASE_URL]];//NetClient繼承自AFN;

? ? ? ? ? ? ? ? ? ?m_netClient.requestSerializer = [AFJSONRequestSerializer serializer];

? ? ? ? ? ? ? ? ? ?m_errorHandler = [[NetErrorHandler alloc] init];

? ? ? ? ? ? ? ? ? ? _isLogin = NO;

}

return self;

}

#pragma mark - Bulletin -

- (void)bulletinListFromOffset:(NSInteger)offset success:(void (^)(NSDictionary *))success failure:(void (^)(NSInteger))failure {

NSDictionary *params = @{@"offset": [NSString stringWithFormat:@"%ld", (long)offset]};

NSString *urlString = @"接口";

[self getRequestWithURL:urlString params:params success:success failure:failure];

}

#pragma mark - Private Methods -

- (void)getRequestWithURL:(NSString *)url params:(id)params success:(void (^)(NSDictionary *))success failure:(void (^)(NSInteger))failure {

? ? ? ? ? m_netClient.responseSerializer = [AFJSONResponseSerializer serializer];

? ? ? ? ?[m_netClient GET:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {

? ? ? ? ?NSDictionary* response = (NSDictionary *)responseObject;

? ? ? ? ? ? ? if (success != nil) {

? ? ? ? ? ? ? ? ? ?success(response);?

? ? ? ? ? ?}

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

? ? ? ? ? ?NSDictionary* response = (NSDictionary *)operation.responseObject;

? ? ? ? ? ?NSInteger errorCode = [[response objectForKey:ERROR_CODE_KEY] integerValue];

? ? ? ? ? ?NSString* errorMessage = [response objectForKey:ERROR_MESSAGE];

? ? ? ? ? ?[self handleError:errorCode message:errorMessage failure:failure];//對錯誤信息的處理

}];

}

在外邊Module: NSObject 中會對dataSource中回調回的數(shù)據(jù)進行具體細化處理,這個文件中的block方法是外面調用的,大部分會在View中調用,屬于MVVM模式。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容