Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/review-apple-io.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@react-native-async-storage/async-storage": patch
---

Keep manifest entries on file read errors and cache large values only after successful writes.

17 changes: 12 additions & 5 deletions packages/async-storage/apple/legacy_storage/RNCAsyncStorage.mm
Original file line number Diff line number Diff line change
Expand Up @@ -603,9 +603,14 @@ - (NSString *)_getValueForKey:(NSString *)key errorOut:(NSDictionary *__autorele
value = [RCTGetCache() objectForKey:key];
if (!value) {
NSString *filePath = [self _filePathForKey:key];
value = RCTReadFile(filePath, key, errorOut);
NSDictionary *readError = nil;
value = RCTReadFile(filePath, key, &readError);
if (value) {
[RCTGetCache() setObject:value forKey:key cost:value.length];
} else if (readError) {
if (errorOut) {
*errorOut = readError;
}
} else {
// file does not exist after all, so remove from manifest (no need to save
// manifest immediately though, as cost of checking again next time is negligible)
Expand Down Expand Up @@ -641,12 +646,14 @@ - (NSDictionary *)_writeEntry:(NSArray<NSString *> *)entry changedManifest:(BOOL
return nil;
}
[value writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
[RCTGetCache() setObject:value forKey:key cost:value.length];
if (error) {
errorOut = RCTMakeError(@"Failed to write value.", error, @{@"key": key});
} else if (_manifest[key] != (id)kCFNull) {
*changedManifest = YES;
_manifest[key] = (id)kCFNull;
} else {
[RCTGetCache() setObject:value forKey:key cost:value.length];
if (_manifest[key] != (id)kCFNull) {
*changedManifest = YES;
_manifest[key] = (id)kCFNull;
}
}
return errorOut;
}
Expand Down