-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCache.m
97 lines (73 loc) · 2.82 KB
/
Cache.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
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
//
// LocalStorage.m
// ProjectVoyage
//
// Created by Gui David on 8/3/22.
//
#import "Cache.h"
#import "GlobalVariables.h"
@import Parse;
@interface Cache ()
@end
@implementation Cache
- (id) init {
self = [super init];
return self;
}
+ (NSMutableOrderedSet *) retrieveCachedMessages:(Chat *)chat {
PFQuery *query = [PFQuery queryWithClassName:MESSAGE_CLASS];
//[PFObject unpinAllObjects];
//[chat unpin];
[query whereKey:@"chatId" equalTo:chat.objectId];
//[query fromPinWithName:chat.objectId];
[query fromLocalDatastore];
[query orderByAscending:@"order"];
// NSArray<Message *> *cachedMessages = (NSArray *)[[query findObjects] copy];
[query findObjectsInBackgroundWithBlock:^(NSArray * _Nullable objects, NSError * _Nullable error) {
//[PFObject unpinAll:objects];
NSArray<Message *> *cachedMessages = objects;
}];
//NSMutableOrderedSet *cachedMessagesSet = [NSMutableOrderedSet orderedSetWithArray:cachedMessages];
// return cachedMessagesSet;
return nil;
}
/* CC - this will be important later, but focusing on messages now
+ (NSArray *) retrieveCachedChats {
PFQuery *query = [PFQuery queryWithClassName:CHAT_CLASS];
[query orderByDescending:UPDATED_AT];
[query fromLocalDatastore];
NSArray *queryKeys = [NSArray arrayWithObjects:RECIPIENTS, CHAT_DESCRIPTION, MESSAGES, IMAGE, nil];
[query includeKeys:queryKeys];
[query whereKey:RECIPIENTS containsAllObjectsInArray:@[[PFUser currentUser]]];
return [query findObjects];
}*/
+ (NSMutableOrderedSet *) retrieveMessagesToSync:(Chat *)chat {
PFQuery *query = [PFQuery queryWithClassName:MESSAGES];
NSString *syncQueueIdentifier = [self getSyncQueueIdentifierForChat:chat];
[query fromPinWithName:syncQueueIdentifier];
[query orderByDescending:ORDER];
[query fromLocalDatastore];
NSArray<Message *> *messagesToSync = [query findObjects];
NSMutableOrderedSet *messagesToSyncSet = [NSMutableOrderedSet orderedSetWithArray:messagesToSync];
return messagesToSyncSet;
}
+ (void) cacheMessagesInSyncQueue:(Message *)message forChat:(Chat *)chat {
NSString *syncQueueIdentifier = [self getSyncQueueIdentifierForChat:chat];
[message pinWithName:syncQueueIdentifier];
}
+ (NSString *) getSyncQueueIdentifierForChat:(Chat *)chat {
return [NSString stringWithFormat:@"%@%@", chat.objectId, @":syncQueue", nil];
}
+ (void) resetCacheWithMessages:messages {
[PFObject unpinAll:messages];
[PFObject pinAll:messages];
}
/* CC - Old strategy of manually caching
+ (BOOL) isCacheFull:(NSMutableOrderedSet *)cachedMessages {
return (cachedMessages.count == STORAGE_SIZE);
}
+ (NSMutableOrderedSet *) removeOldMessagesFromCache:(NSMutableOrderedSet *)cachedMessages {
[cachedMessages reversedOrderedSet];
}
*/
@end