Kariny
Kariny

Reputation: 83

How can I fix my Realm problem for device

I am trying to save my data in a real device and used realm manager something below. My problem is very interesting and I could not even have any idea what is wrong with saving data on my real device. Every simulator works properly and I can save. But it doesn't work on a real device.

Secondly, I could not find the realm DB file when I go to fileURL path it is empty. ( Document file )

(lldb) po Realm.Configuration.defaultConfiguration.fileURL

Can someone help me please ? Full project is here, you can try it yourself if you wish. I would really appreciate it.

import Foundation
import RealmSwift

protocol RealmFactoryProtocol {
    func makeRealm() throws -> Realm
}

final class RealmFactory: RealmFactoryProtocol {
    private let fileManager: FileManagerProtocol
    private let currentSchemaVersion: UInt64 = 1
    private let appID = "com.karinyfreitas.SimpleMarket"

    init(fileManager: FileManagerProtocol = FileManager.default) {
        self.fileManager = fileManager
    }

    private var sharedRealmConfiguration: Realm.Configuration? {
        guard let containerURL = fileManager.containerURL(forSecurityApplicationGroupIdentifier: appID) else {
            return nil
        }

        let configurationURL = containerURL.appendingPathComponent("db.realm", isDirectory: true)

        return Realm.Configuration(
            fileURL: configurationURL,
            schemaVersion: currentSchemaVersion,
            migrationBlock: { _, _ in },
            shouldCompactOnLaunch: { totalBytes, usedBytes in
                // Realm compacting approach from https://realm.io/docs/swift/latest/#compacting-realms
                let oneHundredMB = 100 * 1024 * 1024
                return (totalBytes > oneHundredMB) && (Double(usedBytes) / Double(totalBytes)) < 0.5
            }
        )
    }

    func makeRealm() throws -> Realm {
        guard let configuration = sharedRealmConfiguration else {
            throw RealmFactoryError.unableToMakeConfiguration
        }
        return try Realm(configuration: configuration)
    }
}

enum RealmFactoryError: Error {
    case unableToMakeConfiguration
}

Upvotes: 0

Views: 178

Answers (1)

TiM
TiM

Reputation: 16021

All of that Realm code is correct, so I don't believe there's any problem with how you're creating new Realm objects.

However, I think you've got how containerURL(forSecurityApplicationGroupIdentifier:) works a little confused.

In this example, you're providing your app's bundle identifier to that API, but that's actually not what it expects. It wants a "group identifier" which specifies the name of a shared directory between an app and its extensions, that you define under "App Groups" in your app's Capabilities setting (More info on that is on Apple's developer site).

Looking at the source code, it looks like you've already set up an app group called group.simplemarket.

So if you change

private let appID = "com.karinyfreitas.SimpleMarket"

to

private let appID = "group.simplemarket"

That should fix all of your problems here. Welcome to Stack Overflow!

Upvotes: 3

Related Questions