Code Monkey home page Code Monkey logo

swiftui-core-data-with-model's People

Contributors

berkus avatar shufflingb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

swiftui-core-data-with-model's Issues

Why is it `_model` in the init of AppRoot?

The init of the app occurs as follows

    init() {
        _model = StateObject(wrappedValue: AppModel())
    }

When I set as model, the compiler complains with Cannot assign to property: 'model' is a get-only property

For which reason does adding an underscore in front of the var fix this particular case issue? What are we telling the compiler?

It's by searching the solution on google that I stumbled on this post but I don't understand the implications, nor why it isn't possible in other (more verbose) code.

You can inject @StateObject during initialization by unwrapping the property with _

Is this in the official Apple documentation? How did you find this solution?

Dynamic Object in Fetch Request

This project was super helpful. I didn't understand much of what I read about Combine with Core Data until I looked through your code. Thank you!

I have a question for you. Let's pretend you have another Entity in Core Data called Project. A user can select various projects and view Items within each (one Project, many Items). When they switch the project in the UI, it should update which project is referenced in an NSPredicate here:

extension Item {
    // MARK: - NSFetchRequests for Items
    @nonobjc public class func fetchItemsAll() -> NSFetchRequest<Item> {
        let name = "\(Self.self)"
        print("Running fetch request \(name) for all")
        let request = NSFetchRequest<Item>(entityName: name)
        request.sortDescriptors = []
        request.predicate = NSPredicate(format: "project = %@", project) //<-- predicate here
        return request
    }
}

It seems that once the publishers are set up, they are only set to use whatever is initially defined for project. But if the user changes it, I need the publisher to execute again, perform a new fetch request, and deliver the updated data to my views.

Any idea how I might pull that off? I assume that project would live in its own ObservableObject like this:

class AppState: ObservableObject{
  static let shared = AppState()
  @Published var project: Project!
}

...but I don't understand how to make the publishers fire again based on the project change.

Any ideas?

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.