Skip to content

Commit

Permalink
Fixes #31, corrected issue where requests weeren't being completed
Browse files Browse the repository at this point in the history
  • Loading branch information
comyar committed Nov 18, 2015
1 parent 643cdb9 commit c8107ad
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
31 changes: 15 additions & 16 deletions CZWeatherKit.playground/Contents.swift
Expand Up @@ -26,13 +26,12 @@
import CZWeatherKit
import XCPlayground
/*:
This allows asynchronous operations to complete in our playground.
[NSHipster](http://nshipster.com/xcplayground/#asynchronous-execution) has a
section that goes over this in more detail.
This allows asynchronous operations to complete in our playground.
Set this to `true` to use this playground.
*/
XCPSetExecutionShouldContinueIndefinitely(continueIndefinitely: false)
XCPlaygroundPage.currentPage.needsIndefiniteExecution = false
let APIKEY = ""
/*:
We're going to use this location for the playground.
*/
Expand All @@ -45,14 +44,15 @@ Getting the current conditions from other APIs is just as simple.
*/
var request = CZOpenWeatherMapRequest.newCurrentRequest()
request.location = location
request.key = APIKEY
request.sendWithCompletion { (data, error) -> Void in
if let weather = data {
println("The current weather in Seattle, WA is:")
println("\(weather.current.summary), at \(weather.current.temperature.f)° F")
println("\(weather.current.humidity)% Humidity")
println("Wind speed is \(weather.current.windSpeed.mph) mph, bearing \(weather.current.windDirection)°")
print("The current weather in Seattle, WA is:")
print("\(weather.current.summary), at \(weather.current.temperature.f)° F")
print("\(weather.current.humidity)% Humidity")
print("Wind speed is \(weather.current.windSpeed.mph) mph, bearing \(weather.current.windDirection)°")
} else {
println("Failed to get current weather :(")
print("Failed to get current weather :(")
}
}
/*:
Expand All @@ -62,13 +62,14 @@ the [Open Weather Map API.](http://openweathermap.org/api)
*/
request = CZOpenWeatherMapRequest.newDailyForecastRequestForDays(3)
request.location = location
request.key = APIKEY
request.sendWithCompletion { (data, error) -> Void in
if let weather = data {
for forecast in weather.dailyForecasts {
println("\(forecast.summary), H:\(forecast.highTemperature.f)° L:\(forecast.lowTemperature.f)°)")
print("\(forecast.summary), H:\(forecast.highTemperature.f)° L:\(forecast.lowTemperature.f)°")
}
} else {
println("Something went wrong :(")
print("Something went wrong :(")
}
}

Expand All @@ -81,16 +82,14 @@ can manage your API key if you're calling an API that requires one. For
simplicity, this example doesn't get into all that but feel free to experiment
with those features in this playground.
*/
let service = CZWeatherService()
let service = CZWeatherService(configuration: NSURLSessionConfiguration.defaultSessionConfiguration(), key: APIKEY)
request = CZOpenWeatherMapRequest.newCurrentRequest()
request.location = location
service.dispatchRequest(request, completion: { (data, error) -> Void in
if let weather = data {
for forecast in weather.dailyForecasts {
println("\(forecast.summary), H:\(forecast.highTemperature.f)° L:\(forecast.lowTemperature.f)°)")
}
NSLog("%ld", weather.dailyForecasts.count)
} else {
println("Something went wrong :(")
print("Something went wrong :(")
}
})

Expand Down
8 changes: 5 additions & 3 deletions CZWeatherKit/CZWeatherRequest.m
Expand Up @@ -36,6 +36,8 @@

@interface CZWeatherRequest ()

@property (NS_NONATOMIC_IOSONLY) NSURLSession *session;

@end


Expand All @@ -46,7 +48,7 @@ @implementation CZWeatherRequest
- (instancetype)_init
{
if (self = [super init]) {
// nothing to do
self.session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
}
return self;
}
Expand Down Expand Up @@ -101,10 +103,10 @@ - (void)dispatchWithAPI:(id<CZWeatherAPI>)API
return;
}

[[NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
[[self.session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
CZWeatherData *result = [API transformResponse:response data:data error:error forRequest:copy];
completion(result, error);
}];
}]resume];
}

#pragma mark NSCopying
Expand Down
2 changes: 0 additions & 2 deletions CZWeatherKit/CZWeatherService.m
Expand Up @@ -179,9 +179,7 @@ - (void)dispatchRequests:(NSArray *)requests
dispatch_group_leave(group);
});
}

dispatch_group_wait(group, DISPATCH_TIME_FOREVER);

completion(data, nil);
});
}
Expand Down

0 comments on commit c8107ad

Please sign in to comment.