NSLOG le cache d'une application
Bonjour !
dans mon app, j'essaye de mettre en cache des informations afin que l'utilisateur puisse continuer à utiliser l'app sans réseau disponible.
Je vous donne un bout du code que j'ai écris :
Le log qui cache les id Facebook -
-(void)facebookRequestDidLoad:(id)result{
PFUser *user = [PFUser currentUser];
NSArray *data = [result objectForKey:@data];
if(data){
NSMutableArray *facebookIds = [[NSMutableArray alloc] initWithCapacity:[data count]];
for (NSDictionary *friendData in data ) {
if (friendData[@id]){
[facebookIds addObject:friendData[@id]];
NSLog(@facebookIds : %@", facebookIds);
}
}
[[AACache sharedChache] setFacebookFriends:facebookIds];
if (user) {
if ([user objectForKey:AAUserFacebookFriendsKey]) {
[user removeObjectForKey:AAUserFacebookFriendsKey];
}
NSError *error = nil;
PFQuery *facebookFriendsQuery = [PFUser query];
[facebookFriendsQuery whereKey:AAUserFacebookIDKey containedIn:facebookIds];
NSLog(@liste facebookID : %@", facebookIds);
NSMutableArray *simpleSharingListFriends = [[NSMutableArray alloc] init];
[facebookFriendsQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
NSLog(@comptage : %lu, [objects count]);
[simpleSharingListFriends removeAllObjects];
[simpleSharingListFriends addObjectsFromArray:objects];
NSLog(@liste amis tableau : %@", simpleSharingListFriends);
}
}];
if (!error) {
[simpleSharingListFriends enumerateObjectsUsingBlock:^(PFUser *newFriend, NSUInteger idx, BOOL *stop) {
PFObject *joinActivity = [PFObject objectWithClassName:AAActivityClassKey];
[joinActivity setObject:user forKey:AAActivityFromUserKey];
[joinActivity setObject:newFriend forKey:AAActivityToUserKey];
[joinActivity setObject:AAActivityTypeJoined forKey:AAActivityTypeKey];
PFACL *joinACL = [PFACL ACL];
[joinACL setPublicReadAccess:YES];
joinActivity.ACL = joinACL;
[joinActivity saveInBackground];
NSLog(@liste amis %@", simpleSharingListFriends);
}];
}
[user saveEventually];
}
}
else {
if (user) {
NSString *facebookName = result[@name];
if (facebookName && [facebookName length] != 0) {
[user setObject:facebookName forKey:AAUserNameKey];
NSLog(@facebook Name : %@", facebookName);
}else {
[user setObject:@Someone forKey:AAUserNameKey];
}
NSString *facebookId = result[@id];
if (facebookId && [facebookId length] != 0) {
[user setObject:facebookId forKey:AAUserFacebookIDKey];
NSLog(@facebook Id : %@", facebookId);
}
[user saveEventually];
}
[FBRequestConnection startForMyFriendsWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
[self facebookRequestDidLoad:result];
} else {
[self facebookRequestDidFailWithError:error];
}
}];
}
}
la classe cache qui sert pour le reste de l'appli -
@interface AACache()
@property (strong, nonatomic)NSCache *cache;
-(void)setAttributes:(NSDictionary *)attributes forList:(PFObject *)list;
@end
@implementation AACache
@synthesize cache;
#pragma mark - Init
+(id)sharedChache{
static dispatch_once_t pred = 0;
__strong static id _sharedObject = nil;
dispatch_once(&pred, ^{
_sharedObject = [[self alloc] init];
});
return _sharedObject;
}
-(id)init {
self = [super init];
if (self) {
self.cache = [[NSCache alloc] init];
}
return self;
}
#pragma mark - AACache
-(void)clear {
[self.cache removeAllObjects];
}
#pragma mark - AACache For List
-(void)setAttributesForList:(PFObject *)list sharedWith:(NSArray *)sharers
{
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
@([;sharers count]), kAAListAttributesShareCountKey,
sharers, kAAListAttributesSharersKey,
nil];
[self setAttributes:attributes forList:list];
}
-(NSDictionary *)attributesForList:(PFObject *)list {
NSString *key = [self keyForList:list];
return [self.cache objectForKey:key];
}
-(NSNumber *)shareCountForList:(PFObject *)list {
NSDictionary *attributes = [self attributesForList:list];
if (attributes) {
return [attributes objectForKey:kAAListAttributesShareCountKey];
}
return [NSNumber numberWithInt:0];
}
-(NSArray *)sharersForList:(PFObject *)list {
NSDictionary *attributes = [self attributesForList:list];
if (attributes) {
return [attributes objectForKey:kAAListAttributesSharersKey];
}
return [NSArray array];
}
-(void)incrementSharersCountForList:(PFObject *)list{
NSNumber *sharerCount = [NSNumber numberWithInt:[[self shareCountForList:list] intValue] +1];
NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithDictionary:[self attributesForList:list]];
[attributes setObject:sharerCount forKey:kAAListAttributesShareCountKey];
[self setAttributes:attributes forList:list];
}
-(void)decrementSharersCountForLIst:(PFObject *)list {
NSNumber *sharerCount = [NSNumber numberWithInt:[[self shareCountForList:list] intValue] -1];
if ([sharerCount intValue] < 0) {
return;
}
NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithDictionary:[self attributesForList:list]];
[attributes setObject:sharerCount forKey:kAAListAttributesShareCountKey];
[self setAttributes:attributes forList:list];
}
#pragma mark - AACache For User
-(void)setAttributesForUser:(PFUser *)user listCount:(NSNumber *)count {
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
count, kAAUserAttributesListCountKey,
nil];
[self setAttributes:attributes forUser:user];
}
-(NSDictionary *)attributesForUser:(PFUser *)user {
NSString *key = [self keyForUser:user];
return [self.cache objectForKey:key];
}
-(NSNumber *)listCountForUser:(PFUser *)user{
NSDictionary *attributes = [self attributesForUser:user];
if (attributes) {
NSNumber *listCount = [attributes objectForKey:kAAUserAttributesListCountKey];
if (listCount) {
return listCount;
}
}
return [NSNumber numberWithInt:0];
}
-(void)setListCount:(NSNumber *)count user:(PFUser *)user {
NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithDictionary:[self attributesForUser:user]];
[attributes setObject:count forKey:kAAUserAttributesListCountKey];
[self setAttributes:attributes forUser:user];
}
-(void)setFacebookFriends:(NSArray *)friends {
NSString *key = AAUserDefaultsCacheFacebookFriendsKey;
[self.cache setObject:friends forKey:key];
[[NSUserDefaults standardUserDefaults] setObject:friends forKey:key];
[[NSUserDefaults standardUserDefaults] synchronize];
}
-(NSArray *)facebookFriends {
NSString *key = AAUserDefaultsCacheFacebookFriendsKey;
if ([self.cache objectForKey:key]) {
return [self.cache objectForKey:key];
}
NSArray *friends = [[NSUserDefaults standardUserDefaults] objectForKey:key];
if (friends) {
[self.cache setObject:friends forKey:key];
}
NSLog(@cache : %@", friends);
return friends;
}
#pragma mark - Helper
- (void)setAttributes:(NSDictionary *)attributes forList:(PFObject *)list{
NSString *key = [self keyForList:list];
[self.cache setObject:attributes forKey:key];
}
- (void)setAttributes:(NSDictionary *)attributes forUser:(PFUser *)user {
NSString *key = [self keyForUser:user];
[self.cache setObject:attributes forKey:key];
}
-(NSString *)keyForList:(PFObject *)list {
return [NSString stringWithFormat:@list_%@", [list objectId]];
}
-(NSString *)keyForUser:(PFUser *)user{
return [NSString stringWithFormat:@user_%@", [user objectId]];
}
@end
Par quel moyen est-ce que je peux suivre ce qu'il se passe dans la partie cache de l'application ?
Merci
Connectez-vous ou Inscrivez-vous pour répondre.