iosswiftrealmxcframework

Using RealmSwift in a framework: most symbols found, but not all


I'm building an iOS framework (both a .framework and an .xcframework) which contains some of my source code which uses RealmSwift.

I've found that I can sucessfully build the framework and use it in an app provided it uses a subset of the Realm API, but attempting to include some aspects of the API results in a symbol not found runtime error.

I can, for example, use the framework to open the db, add items, fetch items etc. etc. However if I attempt to reference a Results object then I get:

dyld[11749]: Symbol not found: _$s10RealmSwift7ResultsVyxGSlAASl5countSivgTW

Why might this be?

Here's a bit of code for reference(not complete, sufficient to illustrate), and build settings

public class ObjectA: Object, Codable {
    @objc dynamic var category: String  = ""
    
    override public static func primaryKey() -> String? {
        return "category"
    }
}
    public func addA(key:String) {
        do {
            let objectA = ObjectA()
            objectA.category = key
            
            let realm = try Realm(configuration: configuration!)
            try? realm.write {
                realm.add(objectA, update: Realm.UpdatePolicy.all)
            }
        } catch let error as NSError {
            NSLog(TAG + "ERROR calling add() \(error.code)  \(error.description)")
        }
    }
    public func getAllAs() -> Results<ObjectA> {
        let realm = try! Realm(configuration: configuration!)
        let items = realm.objects(ObjectA.self)
        return items
    }

And some code calling it:

RealmDatabase.instance().addA(key: "something") //ok
let found = RealmDatabase.instance().findObjectA(key: "something") //ok
let allA = RealmDatabase.instance().getAllAs() // ok
let c = allA.count // not ok

If the above 4 lines are included in the framework, that will result in the error. If the last line is omitted, then the code runs successfully.

The pod file for the framework is:

target 'TheFramework' do
  use_frameworks!
  pod 'Realm', '10.49.1'
  pod 'RealmSwift', '10.49.1'
end

post_install do |installer|
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '16.0'
      config.build_settings['BUILD_LIBRARY_FOR_DISTRIBUTION'] = 'YES'
    end
  end
end

The pod file for the app using the framework is:

target 'ReferenceApp' do
  project 'ReferenceApp.xcodeproj'
  use_frameworks!
  pod 'Realm', '10.49.1'
  pod 'RealmSwift', '10.49.1'
end

post_install do |installer|
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '16.0'
    end
  end
end

In the framework Xcode project, the Frameworks & Libraries section for the pods.framework is set to Do Not Embed. Build library for distribution is set to YES.

In the app using the Xcode project, the Frameworks & Libraries section for its pods.framework is similarly set to Do Not Embed, the framework is set to Embed & Sign.

Why can I reference and use 90% of the Realm API, but not 100%. Why does only a small part of the API result in a symbol not found error?


Solution

  • Using Realm with SPM rather than pods seems to eliminate the issue