-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJunctionNotification.m
More file actions
executable file
·117 lines (102 loc) · 3.48 KB
/
JunctionNotification.m
File metadata and controls
executable file
·117 lines (102 loc) · 3.48 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//
// JunctionNotification.m
// Junction
//
// Created by Bobby Ren on 12/28/12.
//
//
#import "JunctionNotification.h"
#import "UserInfo.h"
#define CLASSNAME @"Notification"
@implementation JunctionNotification
@synthesize className, senderPfUserID;
@synthesize type;
@synthesize pfUserID, pfUser, pfObject;
-(id)init {
self = [super init];
[self setClassName:CLASSNAME];
// if we init a userInfo, it must have a new/empty pfObject
// userInfo objects created from Parse are generated by initWithPFObject which uses fromPFObject
PFObject *newPFObject = [[PFObject alloc] initWithClassName:CLASSNAME];
[self setPfObject:newPFObject];
return self;
}
- (id)initWithPFObject:(PFObject *)object {
self = [super init];
if (self)
{
[self fromPFObject:object];
}
return self;
}
-(PFObject*)pfObject {
// returns current object
if (pfObject)
return pfObject;
else {
// do not allocate; returning nil should indicate need to find object
return nil;
}
return pfObject;
}
-(PFObject*)toPFObject {
// for a pulse we only need to save the pfUser and the location
// returns current pfObject but with updated coordinate and user
@try {
[self.pfObject setObject:pfUser forKey:@"pfUser"];
[self.pfObject setObject:pfUserID forKey:@"pfUserID"];
[self.pfObject setObject:senderPfUserID forKey:@"senderPfUserID"];
[self.pfObject setObject:type forKey:@"type"];
}
@catch (NSException *exception) {
NSLog(@"Caught exception in trying to convert UserPulse to PFObject!");
return nil;
}
return self.pfObject;
}
- (id)fromPFObject:(PFObject *)pObject {
[self setPfObject:pObject];
[self setClassName:pObject.className];
[self setPfUser:[pObject objectForKey:@"pfUser"]];
[self setPfUserID:[pObject objectForKey:@"pfUserID"]];
[self setSenderPfUserID:[pObject objectForKey:@"senderPfUserID"]];
[self setType:[pObject objectForKey:@"type"]];
return self;
}
+(void)FindNotificationsForUser:(UserInfo*)userInfo withBlock:(void (^)(NSArray * results, NSError * error))queryCompletedWithResults{
if (!userInfo) {
queryCompletedWithResults(nil, [NSError errorWithDomain:@"com.junction" code:0 userInfo:[NSDictionary dictionaryWithObject:@"No userinfo! Cannot query for pfUserID for null user" forKey:@"message"]]);
return;
}
PFCachePolicy policy = kPFCachePolicyNetworkOnly;
PFQuery * query = [PFQuery queryWithClassName:CLASSNAME];
[query setCachePolicy:policy];
if (!userInfo.pfUserID) {
NSLog(@"No pfUserID! Cannot find notifications for a non-Parse user");
queryCompletedWithResults(nil, nil);
return;
};
NSString * pfUserID = userInfo.pfUserID;
NSLog(@"FindUserPulse using pfUserID %@", pfUserID);
// add user constraint
[query whereKey:@"pfUserID" equalTo:pfUserID];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error) {
NSLog(@"FindUserInfoFromParse: Query resulted in error!");
queryCompletedWithResults(nil, error);
return;
}
else {
if ([objects count] == 0) {
NSLog(@"FindUserInfoFromParse: 0 results");
queryCompletedWithResults(nil, nil);
return;
}
else {
queryCompletedWithResults(objects, error);
return;
}
}
}];
}
@end