-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPhoto+Methods.m
58 lines (36 loc) · 1.65 KB
/
Photo+Methods.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//
// Photo+Methods.m
// FlickrWorld
//
// Created by Nadia Yudina on 3/20/14.
// Copyright (c) 2014 Nadia Yudina. All rights reserved.
//
#import "Photo+Methods.h"
@implementation Photo (Methods)
- (NSString *)description
{
return [NSString stringWithFormat:@"ID: %@ title: %@ urlToLargeImage: %@",self.identifier ,self.title, self.largeImageLink];
}
+ (Photo *)getPhotoFromPhotoDict: (NSDictionary *)photoDict inManagedObjectContext: (NSManagedObjectContext *)context
{
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Photo"];
NSString *searchID = photoDict[@"id"];
NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"identifier==%@",searchID];
fetchRequest.predicate = searchPredicate;
NSArray *photos = [context executeFetchRequest:fetchRequest error:nil];
if ([photos count] == 0) {//if photo not in context
Photo *newPhoto = [NSEntityDescription insertNewObjectForEntityForName:@"Photo" inManagedObjectContext:context];
newPhoto.identifier = [NSString stringWithFormat:@"%@", photoDict[@"id"]];
newPhoto.title = photoDict[@"title"];
newPhoto.largeImageLink = photoDict[@"url_l"];
newPhoto.originalImageLink = photoDict[@"url_o"];
newPhoto.thumbnailLink = photoDict[@"url_t"];
newPhoto.latitude = [NSString stringWithFormat:@"%@", photoDict[@"latitude"]];
newPhoto.longitude = [NSString stringWithFormat:@"%@", photoDict[@"longitude"]];
newPhoto.master = photoDict[@"ownername"];
return newPhoto;
} else {
return photos[0];
}
}
@end