Realm - Add file with initial data to project (iOS/Swift)

后端 未结 7 1416
独厮守ぢ
独厮守ぢ 2020-12-23 12:47

I\'m developing an application for iOS using swift and chose Realm as a database solution for it. I wrote default data in AppDelegate using write/add function from realm doc

相关标签:
7条回答
  • 2020-12-23 13:05

    Implement this function openRealm in AppDelegate and call it in

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
        ...
        openRealm() 
    
        return true
     }
    
    func openRealm() {
    
        let defaultRealmPath = Realm.defaultPath
        let bundleReamPath = NSBundle.mainBundle().resourcePath?.stringByAppendingPathComponent("default.realm")
    
        if !NSFileManager.defaultManager().fileExistsAtPath(defaultRealmPath) {
            NSFileManager.defaultManager().copyItemAtPath(bundleReamPath!, toPath: defaultRealmPath, error: nil)
        }
    }
    

    It will copy your realm file that you bundled in the app to the default realm path, if it doesn't exist already. After that you use Realm normally like you used before.

    There's also the Migration example that you talked about in Swift.

    In Swift 3.0.1 you may prefer this:

        let defaultRealmPath = Realm.Configuration.defaultConfiguration.fileURL!
        let bundleRealmPath = Bundle.main.url(forResource: "seeds", withExtension: "realm")
    
        if !FileManager.default.fileExists(atPath: defaultRealmPath.absoluteString) {
            do {
                try FileManager.default.copyItem(at: bundleRealmPath!, to: defaultRealmPath)
            } catch let error {
                print("error copying seeds: \(error)")
            }
        }
    

    (but please be careful with the optionals)

    0 讨论(0)
  • 2020-12-23 13:05

    And for those that need @pteofil's answer in Objective-c

    - (void)openRealm {
        NSString *defaultRealmPath = [RLMRealm defaultRealm].path;
        NSString *bundleRealmPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"default.realm"];
        if(![[NSFileManager defaultManager] fileExistsAtPath:defaultRealmPath]) {
            [[NSFileManager defaultManager] copyItemAtPath:bundleRealmPath toPath:defaultRealmPath error:nil];
        }
    }
    
    0 讨论(0)
  • 2020-12-23 13:06

    If you want to open it straight from the bundle location and not bother copying it to the default Realm path, look at the implementation here

    0 讨论(0)
  • 2020-12-23 13:12

    Work in the enterprise space, I need to open a Realm for each application without reusing Realm across all applications so I put this together for Swift 3.0. Add this function to the AppDelegate.

    func openRealm()
       {
          let appName = "ApplcationNameGoesHere"
          var rlmConfig = Realm.Configuration()
          let defaultRealmPath = Realm.Configuration.defaultConfiguration.fileURL!
          let appRealmPath = defaultRealmPath.deletingLastPathComponent().appendingPathComponent("\(appName).realm")
          if !FileManager.default.fileExists(atPath: appRealmPath.path) {
         // Use the default directory, but replace the filename with the application name: appName
             rlmConfig.fileURL = rlmConfig.fileURL!.deletingLastPathComponent().appendingPathComponent("\(appName).realm")
    
         }else
          {
             rlmConfig.fileURL = appRealmPath
          }
          // Set this as the configuration used for the default Realm
          Realm.Configuration.defaultConfiguration = rlmConfig      
       }// open the Realm database for the application
    

    The code above opens or creates a Realm with the file name of "ApplicationNameGoesHere.realm" based on the appName variable in this example.

    place

    openRealm() before return true in application: didFinishLaunchingWithOptions
    
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
          // Override point for customization after application launch.
          openRealm()
    
          return true
    

    }

    call it in another class like this:

    let uiRealm = try! Realm()
    
    0 讨论(0)
  • 2020-12-23 13:24

    Updating @pteofil's openRealm function for Swift 2.2/Realm 1.0.2:

    func openRealm() {
    
        let defaultURL =  Realm.Configuration.defaultConfiguration.fileURL!        
        let bundleReamPath = NSBundle.mainBundle().URLForResource("default", withExtension: "realm")
    
    
        if !NSFileManager.defaultManager().fileExistsAtPath(defaultURL.path!) {
            do {
            try NSFileManager.defaultManager().copyItemAtURL(bundleReamPath!,  toURL: defaultURL)
             }
            catch {}
            }
    
    }
    
    0 讨论(0)
  • 2020-12-23 13:28

    Swift version 3, courtesy of Kishikawa Katsumi:

    let defaultRealmPath = Realm.Configuration.defaultConfiguration.fileURL!
    let bundleReamPath = Bundle.main.path(forResource: "default", ofType:"realm")
    
    if !FileManager.default.fileExists(atPath: defaultRealmPath.path) {
    do
    {
        try FileManager.default.copyItem(atPath: bundleReamPath!, toPath: defaultRealmPath.path)
    }
    catch let error as NSError {
        // Catch fires here, with an NSError being thrown
        print("error occurred, here are the details:\n \(error)")
    }
    }
    
    0 讨论(0)
提交回复
热议问题