Skip to content

Commit

Permalink
Merge pull request #1416 from matrix-org/andy/5873_initial_sync
Browse files Browse the repository at this point in the history
Log when filters cannot be saved or loaded
  • Loading branch information
Anderas authored Mar 22, 2022
2 parents 2c6f8d2 + a5c4aa1 commit 59eb834
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 2 deletions.
66 changes: 64 additions & 2 deletions MatrixSDK/Data/Store/MXFileStore/MXFileStore.m
Original file line number Diff line number Diff line change
Expand Up @@ -1652,13 +1652,15 @@ - (void)saveMetaData
#pragma mark - Matrix filters
- (void)loadFilters
{
MXLogDebug(@"[MXFileStore] Loading filters");
NSString *file = [storePath stringByAppendingPathComponent:kMXFileStoreFiltersFile];
filters = [NSKeyedUnarchiver unarchiveObjectWithFile:file];
filters = [self loadObjectOfClasses:[NSSet setWithArray:@[NSDictionary.class, NSString.class]] fromFile:file];

if (!filters)
{
// This is used as flag to indicate it has been mounted from the file
filters = [NSMutableDictionary dictionary];
MXLogDebug(@"[MXFileStore] No filters loaded, creating empty dictionary");
}
}

Expand Down Expand Up @@ -1690,7 +1692,8 @@ - (void)saveFilters
}

// Store new data
[NSKeyedArchiver archiveRootObject:self->filters toFile:file];
MXLogDebug(@"[MXFileStore] Saving filters");
[self saveObject:self->filters toFile:file];
});
}
}
Expand Down Expand Up @@ -2171,6 +2174,65 @@ - (MXFileRoomStore *)roomStoreForRoom:(NSString*)roomId


#pragma mark - Tools

- (void)saveObject:(id)object toFile:(NSString *)file
{
if (@available(iOS 11.0, *))
{
NSError *error;
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:object requiringSecureCoding:NO error:&error];
if (error)
{
MXLogDebug(@"[MXFileStore] Failed archiving root object with error: '%@'", error.localizedDescription);
return;
}

BOOL success = [data writeToFile:file options:0 error:&error];
if (success)
{
MXLogDebug(@"[MXFileStore] Saved data successfully");
}
else
{
MXLogDebug(@"[MXFileStore] Failed saving data with error: '%@'", error.localizedDescription);
}
}
else
{
[NSKeyedArchiver archiveRootObject:object toFile:file];
}
}

- (id)loadObjectOfClasses:(NSSet<Class> *)classes fromFile:(NSString *)file
{
if (@available(iOS 11.0, *))
{
NSData *data = [NSData dataWithContentsOfFile:file];
if (!data)
{
MXLogDebug(@"[MXFileStore] No data to load at file %@", file);
return nil;
}

NSError *error;
id object = [NSKeyedUnarchiver unarchivedObjectOfClasses:classes fromData:data error:&error];
if (object)
{
MXLogDebug(@"[MXFileStore] Loaded object from class");
return object;
}
else
{
MXLogDebug(@"[MXFileStore] Failed loading object from class with error: '%@'", error.localizedDescription);
return nil;
}
}
else
{
return [NSKeyedUnarchiver unarchiveObjectWithFile:file];
}
}

/**
List recursevely files in a folder
Expand Down
5 changes: 5 additions & 0 deletions MatrixSDK/Data/Store/MXMemoryStore/MXMemoryStore.m
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,11 @@ - (void)storeFilter:(nonnull MXFilterJSONModel*)filter withFilterId:(nonnull NSS
filters[filterId] = filter.jsonString;
}

- (NSArray<NSString *> *)allFilterIds
{
return filters.allKeys;
}

- (void)filterWithFilterId:(nonnull NSString*)filterId
success:(nonnull void (^)(MXFilterJSONModel * _Nullable filter))success
failure:(nullable void (^)(NSError * _Nullable error))failure
Expand Down
5 changes: 5 additions & 0 deletions MatrixSDK/Data/Store/MXNoStore/MXNoStore.m
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,11 @@ - (void)storeFilter:(nonnull MXFilterJSONModel*)filter withFilterId:(nonnull NSS
{
}

- (NSArray<NSString *> *)allFilterIds
{
return @[];
}

- (void)filterWithFilterId:(nonnull NSString*)filterId
success:(nonnull void (^)(MXFilterJSONModel * _Nullable filter))success
failure:(nullable void (^)(NSError * _Nullable error))failure
Expand Down
5 changes: 5 additions & 0 deletions MatrixSDK/Data/Store/MXStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,11 @@
*/
- (void)storeFilter:(nonnull MXFilterJSONModel*)filter withFilterId:(nonnull NSString*)filterId;

/**
Retrieve a list of all stored filter ids.
*/
- (nonnull NSArray <NSString *> *)allFilterIds;

/**
Retrieve a filter with a given id.
Expand Down
1 change: 1 addition & 0 deletions MatrixSDKTests/MXFilterTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ - (void)testFilterPermanentStorage

XCTAssertNotNil(filter2);
XCTAssertEqualObjects(filter, filter2);
XCTAssertEqualObjects(fileStore.allFilterIds, @[syncFilterId]);

[expectation fulfill];

Expand Down
1 change: 1 addition & 0 deletions changelog.d/5873.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MXFileStore: Log when filters cannot be saved or loaded

0 comments on commit 59eb834

Please sign in to comment.