Всем привет! Сегодня "освоил" NSURLSession, много еще не понимаю.
Не могу понять где правильно получить кол-во комнат, для построения collection view.
#pragma mark - GET homes
- (void)getRequestHomes
{
__weak DashboardViewController *wSelf = self;
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
NSURL *url = [NSURL URLWithString:@"http://api.plugmee.com/api/Homes"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:20.0];
[request addValue:[NSString stringWithFormat:@"bearer %@", [self.userDefaults objectForKey:@"token"]] forHTTPHeaderField:@"Authorization"];
[request setHTTPMethod:@"GET"];
NSURLSessionDataTask *getDataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData * _Nullable data,
NSURLResponse * _Nullable response,
NSError * _Nullable error)
{
NSLog(@"method getRequestHomes");
wSelf.receivedData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"полученные данные: %@", wSelf.receivedData);
wSelf.resultResponse = [NSString stringWithFormat:@"%@", response];
NSLog(@"ответ сервера: %@", wSelf.resultResponse);
wSelf.homesJSON = [HomesModel arrayOfModelsFromData:data error:nil];
if (wSelf.homesJSON)
{
NSLog(@"Data homes saved");
[wSelf getRequestRooms];
}
}];
[getDataTask resume];
}
#pragma mark - GET rooms
- (void)getRequestRooms
{
__weak DashboardViewController *wSelf = self;
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
NSURL *url = [NSURL URLWithString:@"http://api.plugmee.com/api/rooms/home/15"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:20.0];
[request addValue:[NSString stringWithFormat:@"bearer %@", [self.userDefaults objectForKey:@"token"]] forHTTPHeaderField:@"Authorization"];
[request setHTTPMethod:@"GET"];
NSURLSessionDataTask *getDataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData * _Nullable data,
NSURLResponse * _Nullable response,
NSError * _Nullable error)
{
NSLog(@"method getRequestRooms");
wSelf.receivedData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"полученные данные: %@", wSelf.receivedData);
wSelf.resultResponse = [NSString stringWithFormat:@"%@", response];
NSLog(@"ответ сервера: %@", wSelf.resultResponse);
wSelf.roomsJSON = [RoomsModel arrayOfModelsFromData:data error:nil];
if (wSelf.roomsJSON)
NSLog(@"Data rooms saved");
}];
[getDataTask resume];
}