簡體   English   中英

在iOS中打開Realm文件時出現“ Bad Realm文件頭(#1)”異常

[英]“Bad Realm file header (#1)” Exception when opening Realm file in iOS

我使用的是REALM 0.98.1版(對於Objective C), 有時在應用啟動時壓縮數據庫大小會出現Bad Realm file header ”異常。

以下是AppDelegate應用程序didFinishLaunch中的方法調用序列。

[self setDefaultConfigrutaionForRealm];
[self vacuumRealm];

以下是配置領域的代碼:

+(void)setDefaultConfigrutaionForRealm{
    RLMRealmConfiguration * defCongfig = [RLMRealmConfiguration defaultConfiguration];
    defCongfig.path = REALM_PATH(REALM_FILE_NAME);
    defCongfig.schemaVersion = SCHEMA_VERSION; 
    [RLMRealmConfiguration setDefaultConfiguration:defCongfig];
}

以下是VacuumRealm(壓縮DB大小)的代碼:

+ (void)vacuumRealm {
@try{
    @autoreleasepool {
        RLMRealm *realm = [RLMRealm defaultRealm];
        NSString *realmPath = [realm path];
        NSLog(@"vacuumRealm realmPath = %@", realmPath);

        long long fileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:realmPath error:nil][NSFileSize] longLongValue];
        NSLog(@"vacuumRealm ENTER filesize = %llu", fileSize);

        //
        NSError *err;
        BOOL success;
        NSDate *startDate = [NSDate date];
        NSString *copyPath = [realmPath stringByAppendingString:@".copy"];

        [[NSFileManager defaultManager] removeItemAtPath:copyPath error:&err];
        success = [realm writeCopyToPath:copyPath error:&err];

        if (success) {
            success = [[NSFileManager defaultManager] removeItemAtPath:realmPath error:&err];
            if (success) {
                success = [[NSFileManager defaultManager] copyItemAtPath:copyPath toPath:realmPath error:&err];
                if (success) {
                    [[NSFileManager defaultManager] removeItemAtPath:copyPath error:&err];

                    NSDate *endDate = [NSDate date];
                    NSTimeInterval executionTime = [endDate timeIntervalSinceDate:startDate];
                    NSLog(@"vacuumRealm cleanup took %f ms", executionTime);
                }
            }
        }
        //

        fileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:realmPath error:nil][NSFileSize] longLongValue];
        NSLog(@"vacuumRealm EXIT filesize = %llu", fileSize);
    }
}
@catch (NSException *exception) {
    NSLog(@"Inside vacuumRealm exception = %@",exception.description);
}
@finally {
  }
 }

在調試時,我發現在“ setDefaultConfigrutaionForRealm ”方法中已正確配置了領域路徑(作為參考, 截取了屏幕截圖),但是一旦調用了“ vacuumRealm ”方法,則在下面的行中將獲得“ Bad Realm文件頭(#1) ”:

RLMRealm *realm = [RLMRealm defaultRealm];

屏幕快照顯示-在setDefaultConfigrutaionForRealm方法中,正確設置了領域db路徑。

解決該異常的任何幫助將非常有幫助。

提前致謝。

您正在創建一個Realm實例( RLMRealm *realm = [RLMRealm defaultRealm]; ),並從其下面刪除文件而不釋放實例。 這將導致諸如您看到的損壞之類的問題,因為您在Realm仍在訪問文件時正在修改文件。

這是您方法的更新版本(由於未使用調試日志和err ,因此省略了它):

__block BOOL copySuccess = NO;
NSString *realmPath = [[RLMRealmConfiguration defaultConfiguration] path];
NSString *copyPath = [realmPath stringByAppendingString:@".copy"];
@autoreleasepool {
    [[NSFileManager defaultManager] removeItemAtPath:copyPath error:nil];
    copySuccess = [[RLMRealm defaultRealm] writeCopyToPath:copyPath error:nil];
}
if (copySuccess && [[NSFileManager defaultManager] removeItemAtPath:realmPath error:nil]) {
    [[NSFileManager defaultManager] moveItemAtPath:copyPath toPath:realmPath error:nil];
}

同樣,在此過程中引發的任何異常也不是可恢復的。 因此,在@catch塊中唯一安全的操作是中止。 或者根本沒有@try / @catch子句。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM