你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS-runtime+歸檔保存App數據

iOS-runtime+歸檔保存App數據

編輯:IOS開發綜合

在App開發中,如何保存應用的數據?對於輕量級的數據,可以選擇runtime歸檔。

runtime工具github地址:SerializationKit

 

//
//  YHCacheManager.h
//  PikeWay
//
//  Created by YHIOS002 on 16/6/3.
//  Copyright © 2016年 YHSoft. All rights reserved.
//

#import 
#import "YHWorkGroup.h"
#import "YHABUserInfo.h"
#import "YHIMHandler.h"
#import "ConversationListHandler.h"
#import "YHThirdPModel.h"

@interface YHCacheManager : NSObject

+ (instancetype)shareInstance;

//退出登錄清除緩存
- (void)clearCacheWhenLogout;

#pragma mark - /*******我的名片*********/
//是否要更新
- (BOOL)needUpdateMyCard;
//緩存
- (void)cacheMyCard:(YHUserInfo*)userInfo;
//取出緩存
- (YHUserInfo*)getCacheMyCard;
//清除緩存
- (void)clearCacheMyCard;

#pragma mark - /*******工作圈動態列表*********/
//是否要更新
- (BOOL)needUpdateCacheWorkGroupDynamicList;
//緩存
- (void)cacheWorkGroupDynamicList:(NSArray*)listData;
//取出緩存
- (NSArray*)getCacheWorkGroupDynamiList;
//清除緩存
- (void)clearCacheWorkGroupDynamiList;

//按照動態類型緩存動態列表
- (void)cacheWorkGroupDynamicList:(NSArray*)listData dynamicType:(NSInteger)dynamicType;
//按照動態類型取出緩存
- (NSArray*)getCacheWorkGroupDynamiListWithDynamicType:(NSInteger)dynamicType;

#pragma mark - /*******我的動態列表*********/
//是否要更新
- (BOOL)needUpdateCacheMyDynamicList;
//緩存
- (void)cacheMyDynamciList:(NSArray*)listData;
//取出緩存
- (NSArray*)getCacheMyDynamiList;
//清除緩存
- (void)clearCacheMyDynamiList;

#pragma mark - /*******我的好友列表*********/
//是否要更新
- (BOOL)needUpdateCacheMyFriendsList;
//緩存
- (void)cacheMyFriendsList:(NSArray*)listData;
//取出緩存
- (NSArray*)getCacheMyFriendsList;
//清除緩存
- (void)clearCacheMyFriendsList;

#pragma mark - /*******我的通訊錄*********/
//是否要更新
- (BOOL)needUpdateCacheMyABContactList;
//緩存
- (void)cacheMyABContactList:(NSArray*)listData;
//取出緩存
- (NSArray*)getCacheMyABContactList;
//清除緩存
- (void)clearCacheMyABContactList;


#pragma mark - /*******大咖列表列表*********/
//是否要更新
- (BOOL)needUpdateBigNameList;
//緩存
- (void)cacheBigNameList:(NSArray*)listData;
//取出緩存
- (NSArray*)getCacheBigNameList;
//清除緩存
- (void)clearCacheBigNameList;

#pragma mark - /*******我的訪客*********/
//是否要更新
- (BOOL)needUpdateMyVisitorsList;
//緩存
- (void)cacheMyVisitorsList:(NSArray*)listData;
//取出緩存
- (NSArray*)getCacheMyVisitorsList;
//清除緩存
- (void)clearCacheMyVisitorsList;

#pragma mark - /*******新的好友*********/
//是否要更新
- (BOOL)needUpdateNewFriendsList;
//緩存
- (void)cacheNewFriendsList:(NSArray*)listData;
//取出緩存
- (NSArray*)getCacheNewFriendsList;
//清除緩存
- (void)clearCacheNewFriendsList;

#pragma mark - /*******行業職位列表*********/
//是否要更新
- (BOOL)needUpdateIndustryList;
//緩存
- (void)cacheIndustryList:(NSArray*)listData;
//取出緩存
- (NSArray*)getCacheIndustryList;
//清除緩存
- (void)clearCacheIndustryList;

#pragma mark - /*******評論推送列表*********/
//是否要更新
- (BOOL)needUpdateNotiCommentSingleton;
//緩存
- (void)cacheNotiCommentSingleton:(YHIMHandler *)singleton;
//取出緩存
- (YHIMHandler *)getCacheNotiCommentSingleton;
//清除緩存
- (void)clearCacheNotiCommentSingleton;

#pragma mark - /*******會話列表*********/
//是否要更新
- (BOOL)needUpdateConversationSingleton;
//緩存
- (void)cacheConversationSingleton:(ConversationListHandler *)singleton;
//取出緩存
- (ConversationListHandler *)getCacheConversationSingleton;
//清除緩存
- (void)clearCacheConversationSingleton;

#pragma mark - /*******第三方賬號*********/
- (void)cacheThirdPartyAccount:(YHThirdPModel *)dict;
- (NSMutableDictionary *)getCacheThirdPartyAccountDict;
- (void)clearCacheThirdPartyAccountDict;

@end

 

 

//
//  YHCacheManager.m
//  PikeWay
//
//  Created by YHIOS002 on 16/6/3.
//  Copyright © 2016年 YHSoft. All rights reserved.
//

#import "YHCacheManager.h"
#import 
#import "YHSerializeKit.h"


//有效率時長
static  long const kMyCardValidDuration = 0;
static  long const kWorkgroupDynamicListValidDuration = 3600 * 1;
static  long const kMyDynamicListValidDuration = 3600 * 1;
static  long const kMyFriendsListValidDuration = 60;
static  long const kMyABContactListValidDuration = 0;
static  long const kBigNameListValidDuration = 0;
static  long const kMyVisitorsListValidDuration = 0;
static  long const kNewFriendsListValidDuration = 0;
static  long const kIndustryListValidDuration   = 3600 * 24 * 7;
static  long const kNotiCommentSingletonValidDuration = 3600 * 24 * 30;
static  long const kConversationSingletonValidDuration = 0;

//date key
#define kcacheDateMyCard @"cacheDateMyCard"
#define kcacheDateWorkGroupDynamicList       @"cacheDateWorkgroupDynamicList"
#define kcacheDateMyDynamciList @"cacheDateMyDynamciList"
#define kcacheDateMyfriendsList @"cacheDateMyfriendsList"
#define kcacheDateMyABContactList @"cacheDateMyABContactList"
#define kcacheDateBigNameList @"cacheDateBigNameList"
#define kcacheDateMyVisitorsList @"cacheDateMyVisitorsList"
#define kcacheDateNewFriensList @"cacheDateNewFriensList"
#define kcacheDateIndustryList @"cacheDateIndustryList"
#define kcacheDateNotiCommentSingleton @"cacheDateNotiCommentSingleton"
#define kcacheDateConversationSingleton @"cacheDateConversationSingleton"
#define kcacheDateThirdPartyDict @"cacheDateThirdPartyDict"


//archive key
#define kArchiveKeyMyCard @"ArchiveKeyMyCard"
#define kArchiveKeyWorkGroupDynamicList @"ArchiveKeyWorkGroupDynamicList"
#define kArchiveKeyMyDynamciList @"ArchiveKeyMyDynamciList"
#define kArchiveKeyMyfriendsList @"ArchiveKeyMyfriendsList"
#define kArchiveKeyMyABContactList @"ArchiveKeyMyABContactList"
#define kArchiveKeyBigNameList @"ArchiveKeyBigNameList"
#define kArchiveKeyMyVisitorsList @"ArchiveKeyMyVisitorsList"
#define kArchiveKeyNewFriensList @"ArchiveKeyNewFriensList"
#define kArchiveKeyIndustryList @"ArchiveKeyIndustryList"
#define kArchiveKeyNotiCommentSingleton @"ArchiveKeyNotiCommentSingleton"
#define kArchiveKeyConversationSingleton @"ArchiveKeyConversationSingleton"
#define kArchiveKeyCacheThirdPartyDict @"ArchiveKeyCacheThirdPartyDict"


@interface YHCacheManager ()

@property (nonatomic,strong) YHUserInfo *cacheUserInfo;//我的名片
@property (nonatomic,strong) NSMutableArray *cacheDynamicList;  //工作圈動態
@property (nonatomic,strong) NSMutableArray *cacheMyDynamicList;//我的動態
@property (nonatomic,strong) NSMutableArray *cacheMyFriendsList;//我的好友
@property (nonatomic,strong) NSMutableArray *cacheMyABContactList;//我的通訊錄
@property (nonatomic,strong) NSMutableArray *cacheBigNameList; //大咖列表
@property (nonatomic,strong) NSMutableArray *cacheMyVisitorsList;//我的訪客列表
@property (nonatomic,strong) NSMutableArray *cacheNewFriendsList;//新的好友列表
@property (nonatomic,strong) NSMutableArray *cacheIndustryList;//行業職位列表
@property (nonatomic,strong) YHIMHandler    *cacheNotiCommentSingleton;//評論推送單例
@property (nonatomic,strong) ConversationListHandler *cacheConversationSingleton;//會話列表單例
@property (nonatomic,strong) NSDictionary *workgroupDict;//工作圈動態字典(按照動態類型保存動態)
@property (nonatomic,strong) NSMutableDictionary *thirdPartyDict;

@end

@implementation YHCacheManager

+ (instancetype)shareInstance
{
    static YHCacheManager *g_instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        g_instance = [[YHCacheManager alloc] init];
    });
    return g_instance;
    
}

- (instancetype)init{
    if (self = [super init]) {
       
    }
    return  self;
}

#pragma mark - Private
/**
 *  是否過期
 *
 *  @param cacheKey      緩存時間點的Key
 *  @param validDuration 有限時長
 *
 *  @return YES:過期 NO:有效
 */
- (BOOL)isExpiredWithCacheKey:(NSString *)cacheKey validDuration:(long)validDuration{
    //取出
    NSInteger ts = [[NSUserDefaults standardUserDefaults] integerForKey:cacheKey];
    if (ts == 0) {
        ts = [[NSDate date] timeIntervalSince1970];
        [[NSUserDefaults standardUserDefaults] setObject:@(ts) forKey:cacheKey];
    }
    
    //舊時間日期
    NSDate *oDate = [NSDate dateWithTimeIntervalSince1970:ts];
    //時間間隔
    NSTimeInterval distance = [[NSDate date] timeIntervalSinceDate:oDate];
    
    if (distance > validDuration) {
        return YES;
    }
    return NO;
}

//緩存當前日期
- (void)cacheCurrentDateWithKey:(NSString *)cacheDateKey{
    if(!cacheDateKey)
        return;
    NSTimeInterval interval =  [[NSDate date] timeIntervalSince1970];
    [[NSUserDefaults standardUserDefaults] setObject:@(interval) forKey:cacheDateKey];
}

#pragma mark - Public

- (void)clearCacheWhenLogout{
    [self clearCacheMyCard];
    [self clearCacheMyDynamiList];
    [self clearCacheMyFriendsList];
    [self clearCacheMyVisitorsList];
    [self clearCacheNewFriendsList];
    [self clearCacheNotiCommentSingleton];
    [self clearCacheConversationSingleton];
    [self clearCacheThirdPartyAccountDict];
}

/*******我的名片*********/
#pragma mark - 我的名片
//是否要更新
- (BOOL)needUpdateMyCard{
    if (!self.cacheUserInfo || [self isMyCardExpired]) {
        return YES;
    }
    return NO;
}

//緩存
- (void)cacheMyCard:(YHUserInfo*)userInfo
{
    if (userInfo) {
        self.cacheUserInfo = userInfo;
        //緩存名片
    YHSERIALIZE_ARCHIVE(self.cacheUserInfo,kArchiveKeyMyCard,[self cacheMyCardFilePath]);
        [self cacheCurrentDateWithKey:kcacheDateMyCard];
    }
}

//取出緩存
- (YHUserInfo*)getCacheMyCard
{
    
    YHSERIALIZE_UNARCHIVE(self.cacheUserInfo, kArchiveKeyMyCard, [self cacheMyCardFilePath]);
    return self.cacheUserInfo;
    
}

//清除緩存
- (void)clearCacheMyCard{
    [self deleteFileAtPath:[self cacheMyCardFilePath]];
}

//我的名片過期
- (BOOL)isMyCardExpired{
    
    return [self isExpiredWithCacheKey:kcacheDateMyCard validDuration:kMyCardValidDuration];
}


#pragma mark - 工作圈動態列表
//是否要更新
- (BOOL)needUpdateCacheWorkGroupDynamicList
{
    if (!self.cacheDynamicList.count || [self isWorkGroupDynamicListExpired]) {
        return YES;
    }
    return NO;
}

//緩存
- (void)cacheWorkGroupDynamicList:(NSArray*)listData
{
    
    if (listData.count && listData) {
        self.cacheDynamicList = [listData mutableCopy];
        //緩存工作圈動態
  YHSERIALIZE_ARCHIVE(self.cacheDynamicList,kArchiveKeyWorkGroupDynamicList,[self cacheWorkGroupListFilePath]);
         [self cacheCurrentDateWithKey:kcacheDateWorkGroupDynamicList];
    }
}

//取出緩存
- (NSArray*)getCacheWorkGroupDynamiList{
    
     YHSERIALIZE_UNARCHIVE(self.cacheDynamicList, kArchiveKeyWorkGroupDynamicList, [self cacheWorkGroupListFilePath]);
    return self.cacheDynamicList;
    
}

//清除緩存
- (void)clearCacheWorkGroupDynamiList{
    [self deleteFileAtPath:[self cacheWorkGroupListFilePath]];
}

//工作圈列表過期
- (BOOL)isWorkGroupDynamicListExpired{

    return [self isExpiredWithCacheKey:kcacheDateWorkGroupDynamicList validDuration:kWorkgroupDynamicListValidDuration];
}


//按照動態標簽類型緩存動態列表
- (void)cacheWorkGroupDynamicList:(NSArray*)listData dynamicType:(NSInteger)dynamicType{
    if (listData) {
 
        NSString *cacheKey = [NSString stringWithFormat:@"ArchiveKeyDynList%@",[@(dynamicType) stringValue]];
        NSString *cacheDateKey = [NSString stringWithFormat:@"cacheDateDynList%@",[@(dynamicType) stringValue]];
        //緩存工作圈動態
        YHSERIALIZE_ARCHIVE(listData,cacheKey,[self cacheWorkGroupListFilePathWithDynamicType:dynamicType]);
        [self cacheCurrentDateWithKey:cacheDateKey];

    }
}

//按照動態類型取出緩存
- (NSArray*)getCacheWorkGroupDynamiListWithDynamicType:(NSInteger)dynamicType{
    
    NSArray *retArray = [NSArray new];
    NSString *cacheKey = [NSString stringWithFormat:@"ArchiveKeyDynList%@",[@(dynamicType) stringValue]];
    YHSERIALIZE_UNARCHIVE(retArray, cacheKey, [self cacheWorkGroupListFilePathWithDynamicType:dynamicType]);
    return retArray;
}



#pragma mark - 我的動態列表
//是否要更新
- (BOOL)needUpdateCacheMyDynamicList{
    if (!self.cacheMyDynamicList.count || [self isMyDynamicListExpired]) {
        return YES;
    }
    return NO;
}

//緩存
- (void)cacheMyDynamciList:(NSArray*)listData{
    if (listData.count && listData) {
        self.cacheMyDynamicList = [listData mutableCopy];

  YHSERIALIZE_ARCHIVE(self.cacheMyDynamicList,kArchiveKeyMyDynamciList,[self cacheMyDynamicListFilePath]);
        [self cacheCurrentDateWithKey:kcacheDateMyDynamciList];
    }
}

//取出緩存
- (NSArray*)getCacheMyDynamiList{
   
    YHSERIALIZE_UNARCHIVE(self.cacheMyDynamicList, kArchiveKeyMyDynamciList, [self cacheMyDynamicListFilePath]);
    
    return self.cacheMyDynamicList;
}

//清除緩存
- (void)clearCacheMyDynamiList{
    [self deleteFileAtPath:[self cacheMyDynamicListFilePath]];
}

//我的動態列表過期
- (BOOL)isMyDynamicListExpired{
    
    return [self isExpiredWithCacheKey:kcacheDateMyDynamciList validDuration:kMyDynamicListValidDuration];
}

#pragma mark - 我的好友列表
//是否要更新
- (BOOL)needUpdateCacheMyFriendsList
{
    if (!self.cacheMyFriendsList.count || [self isMyFriendsListExpired]) {
        return YES;
    }
    return NO;
}

//緩存
- (void)cacheMyFriendsList:(NSArray*)listData{
    if (listData) {
        self.cacheMyFriendsList = [listData mutableCopy];
        
        YHSERIALIZE_ARCHIVE(self.cacheMyFriendsList,kArchiveKeyMyfriendsList,[self cacheMyFriendsListFilePath]);
        [self cacheCurrentDateWithKey:kcacheDateMyfriendsList];
    }
}


//取出緩存
- (NSArray*)getCacheMyFriendsList{

    YHSERIALIZE_UNARCHIVE(self.cacheMyFriendsList, kArchiveKeyMyfriendsList, [self cacheMyFriendsListFilePath]);
    return self.cacheMyFriendsList;
}

//清除緩存
- (void)clearCacheMyFriendsList{
    [self deleteFileAtPath:[self cacheMyFriendsListFilePath]];
}

//我的好友列表過期
- (BOOL)isMyFriendsListExpired{
    
    return [self isExpiredWithCacheKey:kcacheDateMyfriendsList validDuration:kMyFriendsListValidDuration];
}

/*******我的通訊錄*********/
#pragma mark - 我的通訊錄
//是否要更新
- (BOOL)needUpdateCacheMyABContactList{
    if (!self.cacheMyABContactList.count || [self isMyABContactListExpired]) {
        return YES;
    }
    return NO;
}

//緩存
- (void)cacheMyABContactList:(NSArray*)listData{
    if (listData.count && listData) {
        self.cacheMyABContactList = [listData mutableCopy];
        
        YHSERIALIZE_ARCHIVE(self.cacheMyABContactList,kArchiveKeyMyABContactList,[self cacheMyABContactListFilePath]);
        [self cacheCurrentDateWithKey:kcacheDateMyABContactList];
    }

}

//取出緩存
- (NSArray*)getCacheMyABContactList{
    
    YHSERIALIZE_UNARCHIVE(self.cacheMyABContactList, kArchiveKeyMyABContactList, [self cacheMyABContactListFilePath]);
    return self.cacheMyABContactList;
}

//清除緩存
- (void)clearCacheMyABContactList{
    [self deleteFileAtPath:[self cacheMyABContactListFilePath]];
}

//我的通訊錄列表過期
- (BOOL)isMyABContactListExpired{
    
    return [self isExpiredWithCacheKey:kcacheDateMyABContactList validDuration:kMyABContactListValidDuration];
}

/*******大咖列表列表*********/
#pragma mark - 大咖列表列表
//是否要更新
- (BOOL)needUpdateBigNameList{
    if (!self.cacheBigNameList.count || [self isBigNameListExpired]) {
        return YES;
    }
    return NO;
}

//緩存
- (void)cacheBigNameList:(NSArray*)listData{
    
    if (listData.count && listData) {
        self.cacheBigNameList = [listData mutableCopy];
        
        YHSERIALIZE_ARCHIVE(self.cacheBigNameList,kArchiveKeyBigNameList,[self cacheBigNameListFilePath]);
        [self cacheCurrentDateWithKey:kcacheDateBigNameList];
    }

    
}

//取出緩存
- (NSArray*)getCacheBigNameList{

    YHSERIALIZE_UNARCHIVE(self.cacheBigNameList, kArchiveKeyBigNameList, [self cacheBigNameListFilePath]);
    return self.cacheBigNameList;
}

//清除緩存
- (void)clearCacheBigNameList{
    [self deleteFileAtPath:[self cacheBigNameListFilePath]];
}

//大咖列表過期
- (BOOL)isBigNameListExpired{
   return [self isExpiredWithCacheKey:kcacheDateBigNameList validDuration:kBigNameListValidDuration];
}

/*******我的訪客*********/
#pragma mark - 我的訪客
//是否要更新
- (BOOL)needUpdateMyVisitorsList{
    if (!self.cacheMyVisitorsList.count || [self isMyVisitorsListExpired]) {
        return YES;
    }
    return NO;
}

//緩存
- (void)cacheMyVisitorsList:(NSArray*)listData{
    
    if (listData.count && listData) {
        self.cacheMyVisitorsList = [listData mutableCopy];
        
        YHSERIALIZE_ARCHIVE(self.cacheMyVisitorsList,kArchiveKeyMyVisitorsList,[self cacheMyVisitorsListFilePath]);
        [self cacheCurrentDateWithKey:kcacheDateMyVisitorsList];
    }

}

//取出緩存
- (NSArray*)getCacheMyVisitorsList{
    
    YHSERIALIZE_UNARCHIVE(self.cacheMyVisitorsList, kArchiveKeyMyVisitorsList, [self cacheMyVisitorsListFilePath]);
    return self.cacheMyVisitorsList;
}

//清除緩存
- (void)clearCacheMyVisitorsList{
    [self deleteFileAtPath:[self cacheMyVisitorsListFilePath]];
}

//我的訪客列表過期
- (BOOL)isMyVisitorsListExpired{
    return [self isExpiredWithCacheKey:kcacheDateMyVisitorsList validDuration:kMyVisitorsListValidDuration];
}

/*******新的好友*********/
#pragma mark - 新的好友
//是否要更新
- (BOOL)needUpdateNewFriendsList{
    if (!self.cacheNewFriendsList.count || [self isNewFriendsListExpired]) {
        return YES;
    }
    return NO;
}

//緩存
- (void)cacheNewFriendsList:(NSArray*)listData{
    if (listData.count && listData) {
        self.cacheNewFriendsList = [listData mutableCopy];
        
        YHSERIALIZE_ARCHIVE(self.cacheNewFriendsList,kArchiveKeyNewFriensList,[self cacheNewFriendsListFilePath]);
         [self cacheCurrentDateWithKey:kcacheDateNewFriensList];
    }
}
//取出緩存
- (NSArray*)getCacheNewFriendsList{
    
    YHSERIALIZE_UNARCHIVE(self.cacheNewFriendsList, kArchiveKeyNewFriensList, [self cacheNewFriendsListFilePath]);
    return self.cacheNewFriendsList;
}

- (void)clearCacheNewFriendsList{
    [self deleteFileAtPath:[self cacheNewFriendsListFilePath]];
}

//新的好友列表過期
- (BOOL)isNewFriendsListExpired{
    return [self isExpiredWithCacheKey:kcacheDateNewFriensList validDuration:kNewFriendsListValidDuration];
}


/*******行業職位列表*********/
#pragma mark - 行業職位列表
//是否要更新
- (BOOL)needUpdateIndustryList{
    if (!self.cacheIndustryList.count || [self isIndustryListExpired]) {
        return YES;
    }
    return NO;
}

//緩存
- (void)cacheIndustryList:(NSArray*)listData{
    if (listData.count && listData) {
        self.cacheIndustryList = [listData mutableCopy];
        
        YHSERIALIZE_ARCHIVE(self.cacheIndustryList,kArchiveKeyIndustryList,[self cacheIndustryListFilePath]);
        [self cacheCurrentDateWithKey:kcacheDateIndustryList];
    }
}

//取出緩存
- (NSArray*)getCacheIndustryList{
  
    YHSERIALIZE_UNARCHIVE(self.cacheIndustryList, kArchiveKeyIndustryList, [self cacheIndustryListFilePath]);
    return self.cacheIndustryList;
}

//清除緩存
- (void)clearCacheIndustryList{
    [self deleteFileAtPath:[self cacheIndustryListFilePath]];
}

//行業職位列表過期
- (BOOL)isIndustryListExpired{
    return [self isExpiredWithCacheKey:kcacheDateIndustryList validDuration:kIndustryListValidDuration];
}

/*******評論推送列表*********/
#pragma mark - 評論推送列表
//是否要更新
- (BOOL)needUpdateNotiCommentSingleton{
    if (!self.cacheNotiCommentSingleton || [self isNotiCommentSingletonExpired]) {
        return YES;
    }
    return NO;
}

//緩存
- (void)cacheNotiCommentSingleton:(YHIMHandler *)singleton{
    if (singleton) {
        self.cacheNotiCommentSingleton = singleton;
        YHSERIALIZE_ARCHIVE(self.cacheNotiCommentSingleton,kArchiveKeyNotiCommentSingleton,[self cacheNotiCommentSingletonFilePath]);
        [self cacheCurrentDateWithKey:kcacheDateNotiCommentSingleton];
    }
}

//取出緩存
- (YHIMHandler *)getCacheNotiCommentSingleton{
    
   YHSERIALIZE_UNARCHIVE(self.cacheNotiCommentSingleton, kArchiveKeyNotiCommentSingleton, [self cacheNotiCommentSingletonFilePath]);
    return self.cacheNotiCommentSingleton;
}

//清除緩存
- (void)clearCacheNotiCommentSingleton{
    [self deleteFileAtPath:[self cacheNotiCommentSingletonFilePath]];
    
    [[YHIMHandler sharedInstance].dataArray removeAllObjects];
    [YHIMHandler sharedInstance].totalBadge = 0;
}

//評論推送列表過期
- (BOOL)isNotiCommentSingletonExpired{
    return [self isExpiredWithCacheKey:kcacheDateNotiCommentSingleton validDuration:kNotiCommentSingletonValidDuration];
}

/*******會話列表*********/
#pragma mark - 會話列表
//是否要更新
- (BOOL)needUpdateConversationSingleton{
    if (!self.cacheConversationSingleton || [self isConversationSingletonExpired]) {
        return YES;
    }
    return NO;
}

//緩存
- (void)cacheConversationSingleton:(ConversationListHandler *)singleton{
    if (singleton) {
        self.cacheConversationSingleton = singleton;
        YHSERIALIZE_ARCHIVE(self.cacheConversationSingleton,kArchiveKeyConversationSingleton,[self cacheConversationSingletonFilePath]);
        [self cacheCurrentDateWithKey:kcacheDateConversationSingleton];
    }
}

//取出緩存
- (ConversationListHandler *)getCacheConversationSingleton{
    YHSERIALIZE_UNARCHIVE(self.cacheConversationSingleton, kArchiveKeyConversationSingleton, [self cacheConversationSingletonFilePath]);
    return self.cacheConversationSingleton;
}

//清除緩存
- (void)clearCacheConversationSingleton{
    [self deleteFileAtPath:[self cacheConversationSingletonFilePath]];
    [[ConversationListHandler sharedInstance].ConversationList removeAllObjects];
}

//評論推送列表過期
- (BOOL)isConversationSingletonExpired{
    return [self isExpiredWithCacheKey:kcacheDateConversationSingleton validDuration:kConversationSingletonValidDuration];
}


#pragma mark - /*******第三方賬號*********/
- (void)cacheThirdPartyAccount:(YHThirdPModel *)model{

    if (!model) {
        DDLog(@"緩存的model為nil");
        return;
    }
    
    //字段的key值與友盟platform名一致!
    NSString *platformName = @"tPAccount";
    switch (model.platformType) {
        case UMSocialPlatformType_QQ:
            platformName = @"qq";
            break;
        default:
            break;
    }
    
    NSMutableDictionary *dictCache = [self getCacheThirdPartyAccountDict];
    if (dictCache) {
        [dictCache setObject:model forKey:platformName];
    }
    else{
        NSMutableDictionary *dictWriteToF = [NSMutableDictionary new];
        [dictWriteToF setObject:model forKey:platformName];
        self.thirdPartyDict = dictWriteToF;
        
        YHSERIALIZE_ARCHIVE(self.thirdPartyDict,kArchiveKeyCacheThirdPartyDict,[self cacheThirdPartyAccountFilePath]);
        [self cacheCurrentDateWithKey:kcacheDateThirdPartyDict];
    }
}

- (NSMutableDictionary *)getCacheThirdPartyAccountDict{
    YHSERIALIZE_UNARCHIVE(self.thirdPartyDict, kArchiveKeyCacheThirdPartyDict, [self cacheThirdPartyAccountFilePath]);
    return self.thirdPartyDict;
}

- (void)clearCacheThirdPartyAccountDict{
    [self deleteFileAtPath:[self cacheThirdPartyAccountFilePath]];
    self.thirdPartyDict = nil;
}

#pragma mark - CachePath
- (BOOL)deleteFileAtPath:(NSString *)filePath{
    if (!filePath || filePath.length == 0) {
        return NO;
    }
    if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
        DDLog(@"delete file error, %@ is not exist!", filePath);
        return NO;
    }
    NSError *removeErr = nil;
    if (![[NSFileManager defaultManager] removeItemAtPath:filePath error:&removeErr] ) {
        DDLog(@"delete file failed! %@", removeErr);
        return NO;
    }
    return NO;
}

- (NSString *)cachePath
{
    NSString *doc = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
    
    NSString *cachePath = [doc stringByAppendingPathComponent:[[NSProcessInfo processInfo] processName]];
    return cachePath;
}

- (NSString *)cachePathInDocument{
    NSURL *docUrl = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] objectAtIndex:0];
    return [docUrl path];
}

- (NSString *)cacheMyCardFilePath{
    return  [[self cachePath ]stringByAppendingPathComponent:@"myCard"];
}

- (NSString *)cacheWorkGroupListFilePath{
   return  [[self cachePath ]stringByAppendingPathComponent:@"wgdynList"];
}

- (NSString *)cacheWorkGroupListFilePathWithDynamicType:(NSInteger)dynamicType{
    return  [[self cachePath ]stringByAppendingPathComponent:[NSString stringWithFormat:@"wgdynList%@",[@(dynamicType) stringValue]]];
}

- (NSString *)cacheMyDynamicListFilePath{
    return  [[self cachePath ]stringByAppendingPathComponent:@"mydynList"];
}

- (NSString *)cacheMyFriendsListFilePath{
    return  [[self cachePath ]stringByAppendingPathComponent:@"myfriendsList"];
}

- (NSString *)cacheMyABContactListFilePath{
    return  [[self cachePath ]stringByAppendingPathComponent:@"myABContactList"];
}

- (NSString *)cacheBigNameListFilePath{
    return  [[self cachePath ]stringByAppendingPathComponent:@"bigNameList"];
}

- (NSString *)cacheMyVisitorsListFilePath{
    return  [[self cachePath ]stringByAppendingPathComponent:@"myVisitorsList"];
}

- (NSString *)cacheNewFriendsListFilePath{
    return  [[self cachePath ]stringByAppendingPathComponent:@"newFriendsList"];
}

- (NSString *)cacheIndustryListFilePath{
    return  [[self cachePath ]stringByAppendingPathComponent:@"industryList"];
}

- (NSString *)cacheNotiCommentSingletonFilePath{
    return  [[self cachePath ]stringByAppendingPathComponent:@"notiCommentSingleton"];
}

- (NSString *)cacheConversationSingletonFilePath{
    return  [[self cachePathInDocument ]stringByAppendingPathComponent:@"conversationSingleton"];
}

- (NSString *)cacheThirdPartyAccountFilePath{
    return [[self cachePath ]stringByAppendingPathComponent:@"thirdPartyAccount"];
}
@end

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved