Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SPM: Realm not found if APPLICATION_EXTENSION_API_ONLY is true on dynamic framework #8291

Closed
GianniCarlo opened this issue Jul 3, 2023 · 5 comments

Comments

@GianniCarlo
Copy link

GianniCarlo commented Jul 3, 2023

How frequently does the bug occur?

Always

Description

  • After adding Realm via SPM to a framework which has APPLICATION_EXTENSION_API_ONLY set to YES, it fails to build after adding a file to it that imports RealmSwift and tries to use it.
  • The error message says: (fatal) module 'Realm' not found, in the sample repo I created, I added a screenshot in the README

Stacktrace & log output

No response

Can you reproduce the bug?

Always

Reproduction Steps

Link to sample project: https://github.com/GianniCarlo/SampleRealmApp/tree/main

  • Create a new project with Realm added via Swift Package Manager
  • Create a new dynamic framework (it could be for iOS or watchOS, it occurs for both)
    • Set APPLICATION_EXTENSION_API_ONLY to YES on its build settings
  • Add a file to the framework
    • Add a class with functions where we just try to access Realm, and fetch/store a new empty model
  • Try to build

This is the sample file I added to the framework

import Foundation
import RealmSwift

public class RealmStore {
  private let storeUrl: URL

  init() {
    let storeUrl = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "identifier.com")!.appendingPathComponent("Store.realm")
    self.storeUrl = storeUrl
  }

  func getRealm() -> Realm {
    return try! Realm(configuration: Realm.Configuration(
      fileURL: storeUrl,
      schemaVersion: 0
    ))
  }

  func getRealmLibrary() -> LibraryRealm {
    let realm = RealmStore().getRealm()

    if let library = realm.objects(LibraryRealm.self).first {
      return library
    }

    let library = LibraryRealm()

    try! realm.write {
      realm.add(library)
    }
    return library
  }
}

class LibraryRealm: Object {}

Version

10.41.0

What Atlas Services are you using?

Local Database only

Are you using encryption?

No

Platform OS and version(s)

Simulator iOS 16.4

Build environment

Xcode version: 14.3.1
Dependency manager and version: SPM

@dianaafanador3
Copy link
Contributor

Hi @GianniCarlo any reason to set this APPLICATION_EXTENSION_API_ONLY to true in your SharedLibrary?. Our SDK doesn't use any API marked in header files with the NS_EXTENSION_UNAVAILABLE macro, or similar unavailability macro, or any API in an unavailable framework, which make it accessible from an extension, and we even set this to true fro installations with Cocoapods. We will investigate a little bit further into this.

@GianniCarlo
Copy link
Author

GianniCarlo commented Jul 11, 2023

Hi @dianaafanador3 ! thanks for the response, it's good to know that it's safe to use Realm for the extensions. I think
at some point a couple of years ago, I had to enable it for the shared framework used in the Watch extension, as I was using some APIs that I shouldn’t have been using, and it wasn't working properly, after finding that flag, I enabled it to avoid a similar scenario in the future. Another reason I have, is to avoid Xcode warnings 😅

I was looking into migrating https://github.com/TortugaPower/BookPlayer from CoreData to Realm, and at first when defining the model, it was building alright, it failed to build when I started writing the fetch queries and the results classes got involved. That's when I created a blank project and compared what was different between the dynamic frameworks and found the flag.

Let me know if I can help with anything else 👌

@tgoyne
Copy link
Member

tgoyne commented Jul 12, 2023

The thing that's failing is the module verifier, which is an optional step that AFAICT is still completely broken if your dependency tree has any c++ in it (as ours does). Disabling it ("Enable Module Verifier" for SharedLibraryKit) makes the build succeed.

@GianniCarlo
Copy link
Author

@tgoyne nice! main project now compiles 😁, and as it's an internal shared library, this totally works for me. I haven't tested the new Xcode beta, but with all the new interoperability things between Swift and C++, hopefully they fixed their module verifier

Thanks again for your help @dianaafanador3 @tgoyne

@shadow-boy
Copy link

The thing that's failing is the module verifier, which is an optional step that AFAICT is still completely broken if your dependency tree has any c++ in it (as ours does). Disabling it ("Enable Module Verifier" for SharedLibraryKit) makes the build succeed.

help me to build success

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants