I posted not too long ago, asking for tips/improvements I could use on my models. I have changed quite a lot, so I thought I'll give it another go.
My model is supposed to handle all the data I get from the Foursquare API:
venueService.h
typedef void (^TNVenueServiceCompletionBlock)(UIImage *image, NSError *error);
@class TNVenueImageData;
@interface TNVenueService : NSObject
@property (nonatomic) NSString *clientID;
@property (nonatomic) NSString *clientSecret;
@property (nonatomic) NSString *radius;
@property (nonatomic) NSArray *venueObject;
@property (nonatomic) NSArray *imageData;
+ (instancetype)venueService;
- (NSDictionary *)mainCategoryKeys;
- (NSArray *)mainCategories;
- (void)performVenueLocationRequest:(CLLocation *)location identifier:(NSString *)identifier;
- (void)performPhotoDetailsRequest:(NSString *)identifier;
- (void)downloadImage:(TNVenueImageData *)imageData completionBlock:(TNVenueServiceCompletionBlock)completion;
@end
venueService.m
@implementation TNVenueService
+ (instancetype)venueService
{
TNVenueService *venueService = [[TNVenueService alloc] init];
venueService.clientID = @"MY_CLIENT_ID";
venueService.clientSecret = @"MY_CLIENT_SECRET";
venueService.radius = @"2000"; //start-radius
return venueService;
}
- (NSURLRequest *)URLRequestWithFormat:(NSString *)attributes, ...
{
va_list arguments;
va_start(arguments, attributes);
NSString *urlPath = [[NSString alloc] initWithFormat:attributes arguments:arguments];
va_end(arguments);
NSURL *url = [NSURL URLWithString:urlPath];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
return request;
}
- (void)performVenueLocationRequest:(CLLocation *)location identifier:(NSString *)identifier
{
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSError *error = nil;
NSURLResponse *response = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:[self buildRequestForVenueLocation:location identifier:identifier]
returningResponse:&response error:&error];
if (!error) {
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSMutableArray *tempArray = [NSMutableArray array];
for (NSDictionary *data in [responseDictionary valueForKeyPath:@"response.venues"]) {
TNVenueObject *venueObject = [[TNVenueObject alloc] initVenueWithName:data[@"name"]
location:data[@"location"]
contact:data[@"contact"]
identifier:data[@"id"]];
[tempArray addObject:venueObject];
}
NSSortDescriptor *distanceSortDiscriptor = [NSSortDescriptor sortDescriptorWithKey:@"distance"
ascending:YES
selector:@selector(localizedStandardCompare:)];
[tempArray sortUsingDescriptors:@[distanceSortDiscriptor]];
_venueObject = [NSArray arrayWithArray:tempArray];
[[NSNotificationCenter defaultCenter] postNotificationName:@"didFinishStoringVenueObject" object:nil];
} else {
NSLog(@"%@", error.localizedDescription);
}
});
}
- (NSDictionary *)mainCategoryKeys
{
return @{@"Arts & Entertainment" : @"4d4b7104d754a06370d81259",
@"Colleges & Universities" : @"4d4b7105d754a06372d81259",
@"Events" : @"4d4b7105d754a06373d81259",
@"Food" : @"4d4b7105d754a06374d81259",
@"Nightlife Spots" : @"4d4b7105d754a06376d81259",
@"Outdoors & Recreation" : @"4d4b7105d754a06377d81259",
@"Professional & Other Places" : @"4d4b7105d754a06375d81259",
@"Residences" : @"4e67e38e036454776db1fb3a",
@"Shops & Services" : @"4d4b7105d754a06378d81259",
@"Travel & Transport" : @"4d4b7105d754a06379d81259"};
}
- (NSArray *)mainCategories
{
return @[@"Arts & Entertainment",
@"Colleges & Universities",
@"Events",
@"Food",
@"Nightlife Spots",
@"Outdoors & Recreation",
@"Professional & Other Places",
@"Residences",
@"Shops & Services",
@"Travel & Transport"];
}
- (void)performPhotoDetailsRequest:(NSString *)identifier
{
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSError *error = nil;
NSURLResponse *response = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:[self buildRequestForVenuePhotoDetails:identifier] returningResponse:&response error:&error];
if (!error) {
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSMutableArray *tempArray = [NSMutableArray array];
if ([[[responseDictionary valueForKeyPath:@"response.venue.photos.groups"] valueForKey:@"items"] count] > 0) {
for (NSDictionary *data in [[responseDictionary valueForKeyPath:@"response.venue.photos.groups"] valueForKey:@"items"][0]) {
TNVenueImageData *imageData = [[TNVenueImageData alloc] initWithPrefix:data[@"prefix"]
width:data[@"width"]
height:data[@"height"]
suffix:data[@"suffix"]
userInfo:data[@"user"]];
[tempArray addObject:imageData];
}
_imageData = [NSArray arrayWithArray:tempArray];
[[NSNotificationCenter defaultCenter] postNotificationName:@"didFinishStoringImageData" object:nil];
} else {
NSLog(@"No Images...");
}
} else {
NSLog(@"%@", error.localizedDescription);
}
});
}
- (void)downloadImage:(TNVenueImageData *)imageData completionBlock:(TNVenueServiceCompletionBlock)completion
{
NSError *error = nil;
NSURL *urlString = [NSURL URLWithString:[self constructImageURL:imageData]];
NSData *data = [NSData dataWithContentsOfURL:urlString options:NSDataReadingUncached error:&error];
__block UIImage *image;
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
image = [UIImage imageWithData:data];
completion(image, error);
});
}
- (NSString *)getTodaysDate
{
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyMMdd"];
return [formatter stringFromDate:date];
}
- (NSString *)constructImageURL:(TNVenueImageData *)imageData
{
return [NSString stringWithFormat:@"%@%@x%@%@", imageData.photoPrefix,
imageData.photoWidth, imageData.photoHeight, imageData.photoSuffix];
}
- (NSURLRequest *)buildRequestForVenueCategory
{
return [self URLRequestWithFormat:@"https://api.foursquare.com/v2/venues/categories?client_id=%@&client_secret=%@&v=%@",
_clientID, _clientSecret, [self getTodaysDate]];
}
- (NSURLRequest *)buildRequestForVenueLocation:(CLLocation *)location identifier:(NSString *)identifier
{
return [self URLRequestWithFormat:@"https://api.foursquare.com/v2/venues/search?ll=%f,%f&radius=%@&categoryId=%@&client_id=%@&client_secret=%@&v=%@", location.coordinate.latitude, location.coordinate.longitude, _radius, identifier, _clientID, _clientSecret, [self getTodaysDate]];
}
- (NSURLRequest *)buildRequestForVenuePhotoDetails:(NSString *)identifier
{
return [self URLRequestWithFormat:@"https://api.foursquare.com/v2/venues/%@?client_id=%@&client_secret=%@&v=%@",
identifier, _clientID, _clientSecret, [self getTodaysDate]];
}
@end
Any further help as to what can be improved once more, would be very helpful! :)