Skip to content

Commit 468b432

Browse files
committed
feat(storage): clear default/current installation's persistent cache
1 parent 87d1e63 commit 468b432

File tree

5 files changed

+46
-7
lines changed

5 files changed

+46
-7
lines changed

AVOS/AVOS.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@
326326
D3A3FA39269ECD40002531C7 /* LCFriendship.h in Headers */ = {isa = PBXBuildFile; fileRef = D3A3FA37269ECD40002531C7 /* LCFriendship.h */; settings = {ATTRIBUTES = (Public, ); }; };
327327
D3A3FA3A269ECD40002531C7 /* LCFriendship.m in Sources */ = {isa = PBXBuildFile; fileRef = D3A3FA38269ECD40002531C7 /* LCFriendship.m */; };
328328
D3AD74AB24BC216200D1BBEE /* LCUserTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = D3AD74AA24BC216200D1BBEE /* LCUserTestCase.swift */; };
329+
D3D41AA9282D226800F6E5CD /* LCInstallationTestCase.swift in Sources */ = {isa = PBXBuildFile; fileRef = D3D41AA8282D226800F6E5CD /* LCInstallationTestCase.swift */; };
329330
/* End PBXBuildFile section */
330331

331332
/* Begin PBXContainerItemProxy section */
@@ -613,6 +614,7 @@
613614
D3A3FA38269ECD40002531C7 /* LCFriendship.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = LCFriendship.m; sourceTree = "<group>"; };
614615
D3AD74AA24BC216200D1BBEE /* LCUserTestCase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LCUserTestCase.swift; sourceTree = "<group>"; };
615616
D3C53FCB2106D84A00D48686 /* LCIMClientProtocol.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = LCIMClientProtocol.h; sourceTree = "<group>"; };
617+
D3D41AA8282D226800F6E5CD /* LCInstallationTestCase.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LCInstallationTestCase.swift; sourceTree = "<group>"; };
616618
D3D6E43923544F520048E58F /* LCGPBExtensionRegistry.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LCGPBExtensionRegistry.m; sourceTree = "<group>"; };
617619
D3D6E43A23544F520048E58F /* LCGPBApi.pbobjc.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LCGPBApi.pbobjc.h; sourceTree = "<group>"; };
618620
D3D6E43B23544F520048E58F /* LCGPBEmpty.pbobjc.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LCGPBEmpty.pbobjc.h; sourceTree = "<group>"; };
@@ -1277,6 +1279,7 @@
12771279
C43D444427281D39006A31FD /* LCTestObject.h */,
12781280
C43D444527281D39006A31FD /* LCTestObject.m */,
12791281
D30B6B6124A0A933006ABE09 /* BaseTestCase.swift */,
1282+
D3D41AA8282D226800F6E5CD /* LCInstallationTestCase.swift */,
12801283
D3AD74AA24BC216200D1BBEE /* LCUserTestCase.swift */,
12811284
C4B25E3C27267DE700F9414F /* LCObjectTestCase.swift */,
12821285
D313717F26BBDAC200123756 /* LCQueryTestCase.swift */,
@@ -1868,6 +1871,7 @@
18681871
files = (
18691872
D30B6B6224A0A933006ABE09 /* BaseTestCase.swift in Sources */,
18701873
D313718026BBDAC200123756 /* LCQueryTestCase.swift in Sources */,
1874+
D3D41AA9282D226800F6E5CD /* LCInstallationTestCase.swift in Sources */,
18711875
C497E8C52760A77300441160 /* LCPushTestCase.swift in Sources */,
18721876
C4B7A99327698C4A00D8833B /* LCIMClientTestCase.swift in Sources */,
18731877
D36A095A25BEA75000A4F312 /* IMMessageTestCase.swift in Sources */,
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// LCInstallationTestCase.swift
3+
// LeanCloudObjcTests
4+
//
5+
// Created by zapcannon87 on 2022/05/12.
6+
// Copyright © 2022 LeanCloud Inc. All rights reserved.
7+
//
8+
9+
import XCTest
10+
@testable import LeanCloudObjc
11+
12+
class LCInstallationTestCase: BaseTestCase {
13+
14+
func testClearPersistentCache() {
15+
let installation = LCInstallation.default()
16+
installation.setDeviceTokenFrom(uuid.data(using: .utf8)!, teamId: "LeanCloud")
17+
XCTAssertTrue(installation.save())
18+
XCTAssertTrue(FileManager.default.fileExists(atPath: LCPersistenceUtils.currentInstallationArchivePath()))
19+
LCInstallation.clearPersistentCache()
20+
XCTAssertFalse(FileManager.default.fileExists(atPath: LCPersistenceUtils.currentInstallationArchivePath()))
21+
}
22+
}

AVOS/LeanCloudObjcTests/LeanCloudObjcTests-Bridging-Header.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@
1414
#import "LCTestObject.h"
1515
#import "LCIMConversation_Internal.h"
1616
#import "LCIMMessage_Internal.h"
17-
17+
#import "LCPersistenceUtils.h"

AVOS/Sources/Foundation/Push/LCInstallation.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ NS_ASSUME_NONNULL_BEGIN
4747
/// For compatibility, same as the `defaultInstallation`.
4848
+ (instancetype)currentInstallation;
4949

50+
/// Clear default/current installation's Persistent Cache.
51+
+ (void)clearPersistentCache;
52+
5053
/// Create a new installation instance.
5154
+ (instancetype)installation;
5255

AVOS/Sources/Foundation/Push/LCInstallation.m

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#import "LCPersistenceUtils.h"
1212
#import "LCErrorUtils.h"
1313
#import "LCRouter_Internal.h"
14+
#import "LCLogger.h"
1415

1516
@implementation LCInstallation {
1617
NSString *_deviceToken;
@@ -43,12 +44,10 @@ + (instancetype)defaultInstallation
4344
dispatch_once(&onceToken, ^{
4445
instance = [LCInstallation installation];
4546
NSString *path = [LCPersistenceUtils currentInstallationArchivePath];
46-
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
47-
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithDictionary:
48-
[LCPersistenceUtils getJSONFromPath:path]];
49-
if (dictionary) {
50-
[LCObjectUtils copyDictionary:dictionary
51-
toObject:instance];
47+
if (path && [[NSFileManager defaultManager] fileExistsAtPath:path]) {
48+
NSDictionary *dictionary = [LCPersistenceUtils getJSONFromPath:path];
49+
if ([dictionary isKindOfClass:[NSDictionary class]]) {
50+
[LCObjectUtils copyDictionary:dictionary toObject:instance];
5251
}
5352
}
5453
});
@@ -60,6 +59,17 @@ + (instancetype)currentInstallation
6059
return [LCInstallation defaultInstallation];
6160
}
6261

62+
+ (void)clearPersistentCache {
63+
NSString *path = [LCPersistenceUtils currentInstallationArchivePath];
64+
if (path && [[NSFileManager defaultManager] fileExistsAtPath:path]) {
65+
NSError *error = nil;
66+
[[NSFileManager defaultManager] removeItemAtPath:path error:&error];
67+
if (error) {
68+
LCLoggerError(LCLoggerDomainStorage, @"Error: %@", error);
69+
}
70+
}
71+
}
72+
6373
+ (NSString *)deviceType
6474
{
6575
#if TARGET_OS_TV

0 commit comments

Comments
 (0)