Code Monkey home page Code Monkey logo

Comments (6)

sync-by-unito avatar sync-by-unito commented on September 24, 2024

➤ PM Bot commented:

Jira ticket: RCOCOA-2380

from realm-swift.

nirinchev avatar nirinchev commented on September 24, 2024

Hey, can you share the full repro project - it appears like someone else is opening a Realm with the same identifier but a different schema, so the schema validation logic kicks in, but it's not obvious from the shared code what could be culprit.

from realm-swift.

Jaycyn avatar Jaycyn commented on September 24, 2024

Thanks @nirinchev. The entire project is the above code - it's local only so there would not be someone else opening the realm and there are no other apps accessing that projects directory or supporting files directory.

I do have a followup question that may lead to the solution.

Keep in mind that the project itself works, it's only the preview that's crashing

#Preview {
    return ContentView()
        .environment(\.realm, ItemGroup.previewRealm)  // <----- Causes crash
}

When a new, fresh project is first run, Realm creates the default.realm file with the objects schema. If the app is then quit, and the schema is changed in the project but the project is NOT run, the only 'code' that's doing anything is the XCode code that drives the preview with the injected .environment

The preview should only be getting it's data from the ItemGroup.previewRealm so even if the objects schema were to change, if the preview is refreshed, it would still pull data only from the realm defined in the previewRealm, right?

So what would trigger a migration if the app itself is not being run?

from realm-swift.

nirinchev avatar nirinchev commented on September 24, 2024

I can't seem to repro this with the code provided. I can see a preview with a globe and "Hello world" below it and even if I update it to read some data from the Realm file, it still works just fine. Is it possible you have another instance of Xcode/your app opening an in-memory realm with the same identifier somewhere?

from realm-swift.

Jaycyn avatar Jaycyn commented on September 24, 2024

@nirinchev Yes, I have the same result. However, do this: Add a property to the Item object but DO NOT RUN the project. Let the preview refresh and it will error out.

Here's the Item object (per the original report above)

class Item: Object, ObjectKeyIdentifiable {
    @Persisted(primaryKey: true) var _id: ObjectId
    @Persisted var itemName = ""
    @Persisted var isFavorite = false
    @Persisted var ownerId = ""
}

and then add a property - do NOT run the project

class Item: Object, ObjectKeyIdentifiable {
    @Persisted(primaryKey: true) var _id: ObjectId
    @Persisted var itemName = ""
    @Persisted var isFavorite = false
    @Persisted var ownerId = ""
    @Persisted var test = ""  // added property
}

and preview crashes.

from realm-swift.

Jaycyn avatar Jaycyn commented on September 24, 2024

@nirinchev I am still getting the same error as in the original post - and I am narrowing the issue and think it can be duplicated.

One thing I noted is that on a 100% clean project, using the above code, without running the project, the #Preview generates the Realm files on disk. This apparently means that during development, changing an object schema, even if the added properties are not used anywhere, will crash preview without actually running any code.

  1. Create a new macOS app, SwiftUI

  2. Add Realm via SPM (per the documentation)

  3. copy and paste the code in the Content View. Do NOT run the project

  4. Preview will not work at this point. Go to the Project Target settings and delete App Sandbox and Hardened Runtime

  5. Upon going back to the ContentView, Preview will refesh and show the globe with Hello, World. If you then look at ~/Library/Application Support/your project name, you will see the Realm files have been created - even though the app has not been run

  6. In the ContentView file, go to the Item object model and add a property @Persisted var test = "" so it looks like this

class Item: Object, ObjectKeyIdentifiable {
    @Persisted(primaryKey: true) var _id: ObjectId
    @Persisted var itemName = ""
    @Persisted var isFavorite = false
    @Persisted var ownerId = ""
    @Persisted var test = ""  //add this
}
  1. At that point, preview will crash with a Fatal Error

Realm_Preview_Crash_PreviewReplacement_ContentView_1/ContentView.swift:71: Fatal error: Can't bootstrap item data: Migration is required due to the following errors:
- Property 'Item.test' has been added.
]

Then, delete the realm files that were generated:

  1. delete the folder at ~/Library/Application Support/your project name

  2. refresh preview, and it still crashes, even though the Realm files were deleted.

I thought perhaps adding a Migration (per the documentation) would help but without running the project, that migration doesn't run either.

Deleting the realm folder and cleaning the build folder doesn't help.

I did the above steps 4 times with the same result - My thinking is the preview should not be accessing any files on disk as an in memory realm is being injected. It's not clear (to me) what's causing the issue or how to handle changing an object model during development and have preview work.

from realm-swift.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.