Code Monkey home page Code Monkey logo

mongorealmexample's Introduction

MongoRealmExample

CI Status

Goal

Quick Start with SwiftUI and Combine

A sample project to showcase Realm Database integration with SwiftUI and Combine.

Screenshots

Items List Add/Remove Details
Items List Add/Remove Item Details

Roadmap

  • Realtime sync with BE
  • Login screen
  • Different data types
  • Guideline on setting up the MongoDB

Installation

Manual

  1. Clone the repo
  2. Run the app

Usage

Folder sturcture

.
├── Application                 # ContentView: SwiftUI.App, Configuration
├── Models                      # Data Models
├── Views                       # Views
├── ReusableComponents          # Design system reusable components (in progress)
├── Resources                   # Assets, TestData, SwiftUI Preview Content, Info.plist
└── README.md
Example code Screenshot

Items List (ItemsListView.swift)

  List {
      // ⚠️ ALWAYS freeze a Realm list while 
      // iterating in a SwiftUI View's ForEach(). 
      // Otherwise, unexpected behavior will occur,
      // especially when deleting object from the list.
      ForEach(items.freeze()) { frozenItem in
          // "Thaw" the item before passing it in, as ItemRow
          // may want to edit it, and 
          // cannot do so on a frozen object.
          // This is a convenient place 
          // to thaw because we have access
          // to the unfrozen realm via the items list.
          ItemRow(item: items.realm!
                .resolve(ThreadSafeReference(to: frozenItem))!)
      }
      .onDelete(perform: delete) // swipe to delete action
      .onMove(perform: move) // move items in editing mode
  }
  
Items List

Delete an item

  // Deletes the given item.
  func delete(at offsets: IndexSet) {
     guard let realm = items.realm else {
          items.remove(at: offsets.first!)
          return
      }
      try! realm.write {
          realm.delete(items[offsets.first!])
      }
  }
  
Delete an item

Add/update item name (ItemDetailsView.swift)

  // Ensure the item was thawed before passing in
  init(_ item: Item) {
    assert(!item.isFrozen)
  }
  
  // Whenever a user performs an action 
  // like pressing the return key, 
  // TextField calls onCommit closure
  TextField(item.name, text: $newItemName, onCommit: { 
    self.commit() 
  })
  
  // Writes the given name to the realm in a transaction.
  private func commit() {
     guard let realm = item.realm else {
          item.name = newItemName
          return
      }
      try! realm.write {
          item.name = newItemName
      }
  }
  
Add/update item name

Toggle favorite (ItemDetailsView.swift)

  // ⚠️ We cannot use the item property 
  // directly, as sets will not
  // automatically run in a transaction. 
  // Here we provide a custom
  // binding to handle the update in a transaction.
  Toggle(isOn: Binding(get: {
      // Return the value as normal.
      item.isFavorite
  }, set: { (value) in
      // If the item is associated with a realm,
      // open a transaction on it in order to do
      // the write.
      guard let realm = item.realm else {
          item.isFavorite = value
          return
      }
      try! realm.write {
          item.isFavorite = value
      }
  })) {
      // 💡 It might have been nice to use a Button instead
      // of a Toggle, but that wouldn't demonstrate custom bindings.
      Image(systemName: item.isFavorite ? "heart.fill" : "heart")
  }
  
Toggle favorite

Contacts

Email: [email protected]

Website: mogoni.dev

Acknowledgements and Third-Party Copyrights

References

© MongoDB, Inc 2008-present. MongoDB, Mongo, and the leaf logo are registered trademarks of MongoDB, Inc.

mongorealmexample's People

Contributors

armenm avatar amkrt02 avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

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.