Inventory Management System with Swift + Realm

As there aren’t many real tutorials for a production ready app, here you go.

To use MongoDB Realm with Swift to create an inventory management system, you can follow these general steps:

  1. Set up a MongoDB Realm app and configure it to work with your Swift app.

  2. Create a data model in Swift that represents your inventory items. You can use the @objcMembers attribute to expose the Swift properties to Objective-C.

    import RealmSwift
    
    @objcMembers
    class InventoryItem: Object {
        dynamic var id = UUID().uuidString
        dynamic var name = ""
        dynamic var quantity = 0
        dynamic var price = 0.0
    }
    
  3. Create a Realm database and add some sample data.

    let config = Realm.Configuration(schemaVersion: 1)
    Realm.Configuration.defaultConfiguration = config
    let realm = try! Realm()
    
    try! realm.write {
        let item1 = InventoryItem()
        item1.name = "Product 1"
        item1.quantity = 10
        item1.price = 9.99
        realm.add(item1)
    
        let item2 = InventoryItem()
        item2.name = "Product 2"
        item2.quantity = 5
        item2.price = 19.99
        realm.add(item2)
    }
    
  4. Retrieve the inventory items from the Realm database and display them in a table view.

    let inventoryItems = realm.objects(InventoryItem.self)
    
    // Implement table view delegate and data source methods to display inventory items
    extension InventoryViewController: UITableViewDelegate, UITableViewDataSource {
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return inventoryItems.count
        }
        
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "InventoryCell", for: indexPath)
            let item = inventoryItems[indexPath.row]
            cell.textLabel?.text = item.name
            cell.detailTextLabel?.text = "Qty: \(item.quantity) | Price: \(item.price)"
            return cell
        }
    }
    

    In this example, we retrieve the inventory items from the Realm database and display them in a table view. We use the objects(_:) method to retrieve all InventoryItem objects in the Realm database. We implement the UITableViewDelegate and UITableViewDataSource protocols to display the inventory items in the table view.

  5. Implement functionality to add, edit, and delete inventory items.

    You can implement functionality to add, edit, and delete inventory items. For example, you can create a form view to add new inventory items and a detail view to edit existing inventory items.

    // Add a new inventory item
    let newItem = InventoryItem()
    newItem.name = "Product 3"
    newItem.quantity = 7
    newItem.price = 14.99
    
    try! realm.write {
        realm.add(newItem)
    }
    
    // Edit an existing inventory item
    let itemToEdit = inventoryItems[0]
    
    try! realm.write {
        itemToEdit.quantity = 15
        itemToEdit.price = 12.99
    }
    
    // Delete an inventory item
    let itemToDelete = inventoryItems[1]
    
    try! realm.write {
        realm.delete(itemToDelete)
    }
    

    In this example, we add a new inventory item by creating a new InventoryItem object and adding it to the Realm database using the add(_:) method inside a write transaction. We edit an existing inventory item by retrieving it from the Realm database and updating its properties inside a write transaction. We delete an inventory item by retrieving it from the Realm database and calling the delete(_:) method inside a write transaction.

Realm database and calling the delete(_:) method inside a write transaction.

  1. Implement functionality to query the inventory items based on various criteria.

    You can use the filter(_:) method to query the inventory items based on various criteria. For example, you can filter the inventory items by name, quantity, or price.

    // Filter inventory items by name
    let filteredItems = realm.objects(InventoryItem.self).filter("name contains[c] %@", "Product")
    
    // Filter inventory items by quantity
    let lowQuantityItems = realm.objects(InventoryItem.self).filter("quantity < 5")
    
    // Filter inventory items by price
    let highPriceItems = realm.objects(InventoryItem.self).filter("price > 10.0")
    

    In this example, we use the filter(_:) method to query the inventory items based on name, quantity, and price. We use the contains[c] operator to perform a case-insensitive search for items whose name contains the string “Product”. We use the < and > operators to filter items based on their quantity and price.

  2. Add synchronization with MongoDB Atlas to enable real-time collaboration and offline access.

    You can add synchronization with MongoDB Atlas to enable real-time collaboration and offline access. You can use the SyncUser.current property to get the current user and the SyncConfiguration class to configure the synchronization.

    let user = SyncUser.current!
    let config = SyncConfiguration(user: user, partitionValue: "inventory")
    let realm = try! Realm(configuration: config)
    

    In this example, we get the current user using the SyncUser.current property and configure the synchronization using the SyncConfiguration class. We set the partition value to “inventory” to synchronize only the inventory items for the current user.

By following these steps, you can use MongoDB Realm with Swift to create an inventory management system with features such as data modeling, CRUD operations, querying, and synchronization.