Need help with SwiftDB?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

vmanot
208 Stars 5 Forks MIT License 102 Commits 0 Opened issues

Description

A modern database abstraction layer, batteries included.

Services available

!
?

Need anything else?

Contributors list

# 14,715
Swift
iOS
attribu...
compati...
101 commits

SwiftDB

A type-safe, SwiftUI-inspired wrapper around CoreData.

Get Started

Define an

Entity
:
struct Foo: Entity, Identifiable {
    @Attribute var bar: String = "Untitled"

var id: some Hashable {
    bar
}

}

Define a

Schema
:
struct MySchema: Schema {
    var entities: Entities {
        Foo.self
    }
}

Create a

ContentView
for your application:
struct ContentView: View {
    @StateObject var container = PersistentContainer(MySchema())

var body: some View {
    Text("Hello World")
}

}

Create a list view:

struct ListView: View {
    @EnvironmentObject var container: PersistentContainer

@QueryModels<foo>() var models

var body: some View {
    NavigationView {
        List(models) { foo in
            NavigationLink(destination: EditView(foo: foo)) {
                Text(foo.bar)
            }
            .contextMenu {
                Button {
                    container.delete(foo)
                } label: {
                    Text("Delete")
                }
            }
        }
        .navigationBarItems(
            trailing: Button {
                container.create(Foo.self)
            } label: {
                Image(systemName: .plusCircleFill)
                    .imageScale(.large)
            }
        )
        .navigationBarTitle("A List of Foo")
    }
}

struct EditView: View {
    @EnvironmentObject var container: PersistentContainer

    let foo: Foo

    var body: some View {
        VStack {
            Form {
                TextField("Enter a value", text: foo.$bar) {
                    container.save()
                }
            }
        }
        .navigationBarTitle("Edit Foo")
    }
}

}

Add it to our

ContentView
:
struct ContentView: View {
    @StateObject var container = PersistentContainer(MySchema())

var body: some View {
    ListView()
        .persistentContainer(container)
}

}

That's it.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.