gpt4 book ai didi

ios - 复制 Realm 文件似乎会导致 Realm 迁移 block 调用 - 为什么?

转载 作者:行者123 更新时间:2023-11-29 02:28:57 25 4
gpt4 key购买 nike

将 .realm 文件从 folder-location1 复制到 folder-location2(并使用 NSFileManager 将其名称从 location1.realm 更改为 location2.realm)之后 - 似乎在这样做之后,第一次调用这个新文件导致迁移 block 调用! (即 setSchemaVersion)。我想知道为什么 ???

之前对“location1/location1.realm”文件的调用不会导致迁移 block 调用 - 但是对“location2/location2.realm”的调用会 - 并且文件结构相同(至少在 Realm-Browser 中没有明显区别)!!

这是我的代码:

    ApplicatonSuppPathHandler *ApplicationSupportPathHandler = [[ApplicatonSuppPathHandler alloc] init];

// creation of location where this directory shall be placed on the iPhone
// typically /library/Application Support/<bundleID_name>/RLMGeneralDatabasesFolderName/...
// = name of directory that the .realm-File finally should be placed in
NSString *RLMLocation1DirectoryName = folderName;

// if it does not exist already, create the RLMLocation_xyz-directory (.../library/Application Support/<bundleID_name>/RLMDatabasesFolderName)
if([ApplicationSupportPathHandler getURLToApplicationDirectoryWithSubDirectory:RLMLocation1DirectoryName] == nil) {
[ApplicationSupportPathHandler createSubDirectoryAtLocationToApplicationDirectory:RLMLocation1DirectoryName];
}

// get the name of entire directory just created
NSURL *RLMLocation1Directory = [ApplicationSupportPathHandler getURLToApplicationDirectoryWithSubDirectory:RLMLocation1DirectoryName];

// name of entire path-name (including filename !! ...needed for copy function below...)
NSString *RLMLocation1Path = [[RLMLocation1Directory path] stringByAppendingPathComponent:fileName];

// HERE IS WHERE THE MIGRATION BLOCK IS CALLED - WHY ?????
// *******************************************************
RLMRealm *realm_Location1 = [RLMRealm realmWithPath:RLMLocation1Path]; // pointing to realm file at path


// the rest does work after the migration-block call...
[realm_Location1 beginWriteTransaction];
[realm_Location1 deleteAllObjects];
[realm_Location1 addObject:RLMTopoRes];
[realm_Location1 commitWriteTransaction];

下面是使用的 ApplicationSuppPathHandler 类方法的实现:

@implementation ApplicatonSuppPathHandler

- (NSURL*)getURLToApplicationDirectoryWithSubDirectory:(NSString*)SubDirectoryName {

NSString* appBundleID = [[NSBundle mainBundle] bundleIdentifier];
NSFileManager*fm = [NSFileManager defaultManager];
NSURL* dirPath = nil;

// Find the application support directory in the home directory.
NSArray* appSupportDir = [fm URLsForDirectory:NSApplicationSupportDirectory
inDomains:NSUserDomainMask];
if ([appSupportDir count] > 0)
{
// Append the bundle ID and the location-Foldername to the URL for the Application Support directory
dirPath = [[[appSupportDir objectAtIndex:0] URLByAppendingPathComponent:appBundleID] URLByAppendingPathComponent:SubDirectoryName];

BOOL isDir;
BOOL exists = [fm fileExistsAtPath:[dirPath path] isDirectory:&isDir];
if (exists) {
/* file exists */
if (isDir) {
/* path exists */
return dirPath;
}
else {
NSLog(@"Directory does not exist");
return nil;
}
}
else {
/* file does not exist */
return nil;
}
}
return dirPath;
}

- (void)createSubDirectoryAtLocationToApplicationDirectory:(NSString*)SubDirectoryName {

NSString* appBundleID = [[NSBundle mainBundle] bundleIdentifier];
NSFileManager*fm = [NSFileManager defaultManager];
NSURL* dirPath = nil;

// Find the application support directory in the home directory.
NSArray* appSupportDir = [fm URLsForDirectory:NSApplicationSupportDirectory
inDomains:NSUserDomainMask];
if ([appSupportDir count] > 0)
{
// Append the bundle ID and the location-Foldername to the URL for the Application Support directory
dirPath = [[[appSupportDir objectAtIndex:0] URLByAppendingPathComponent:appBundleID] URLByAppendingPathComponent:SubDirectoryName];

// If the directory does not exist, this method creates it.
// This method call works in OS X 10.7 and later only.
NSError* theError = nil;
if (![fm createDirectoryAtURL:dirPath withIntermediateDirectories:YES attributes:nil error:&theError]) {
// Handle the error.
NSLog(@"%@", theError.localizedDescription);
}
else {
// Mark the directory as excluded from iCloud backups
if (![dirPath setResourceValue:@YES
forKey:NSURLIsExcludedFromBackupKey
error:&theError]) {
NSLog(@"Error excluding %@ from iCloud backup %@", [dirPath lastPathComponent], theError.localizedDescription);
}
else {
// NSLog(@"Location Directory excluded from iClud backups");
}
}
}
}

最佳答案

之前 Realm 中有一个错误,新的 Realm 文件会将其模式版本设置为 0 而不是当前模式版本,这会在您的情况下触发迁移。

上周 Realm 的 master 分支修复了这个问题:https://github.com/realm/realm-cocoa/pull/1142

Building Realm from master 应该可以解决您遇到的问题。 Realm v0.88.0 将随此修复很快发布。

关于ios - 复制 Realm 文件似乎会导致 Realm 迁移 block 调用 - 为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27153294/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com