Comments (9)
Hi @innoreq! Let me explain. You need to call something like this:
public func remove(_ item: T) throws {
try self.persistentContainer.perform { context in
try context.delete(T.all.where(\T.id == item.id))
}
}
delete method where argument is ManagedObject typically uses when you first fetch some objects from db:
public func remove(_ item: T) throws {
try self.persistentContainer.perform { context in
let objects = try ctx.fetch(T.all.where(\T.id == item.id))
try objects.forEach { try context.delete($0) }
}
}
from sworm.
Thanks for your explanation. Makes things much clearer to me.
I now changed the repo this way (according to suggestion #1):
public func remove(_ item: T) throws {
try self.persistentContainer.perform { context in
guard let uuid = item.uuid else { return }
try context.delete(request: T.all.where(\T.uuid == uuid))
}
I'm calling that from a test:
func testStore() throws {
let store = Store(type: .inMemory,
seeder: nil)
let repo: BaseRepository<Country> = store.makeRepository(for: .country)
let allBefore = try repo.allItems()
XCTAssertEqual(allBefore.count, 0)
let new: Country = .init()
try repo.create(new)
let allAfter = try repo.allItems()
XCTAssertEqual(allAfter.count, 1)
XCTAssertEqual(allAfter[0], new)
try repo.remove(new)
let allRemaining = try repo.allItems()
XCTAssertEqual(allRemaining.count, 0)
}
This fails with a fatal:
Thread 1: Fatal error: unsafelyUnwrapped of nil optional
that happens here:
extension ManagedObjectConvertible {
static func attribute(_ keyPath: PartialKeyPath<Self>) -> Attribute<Self> {
self.attributes.first(where: { $0.keyPath == keyPath }).unsafelyUnwrapped
}
...
because self.attributes.first() returns nil
which obviously cannot be unsafelyUnwrapped
.
Am I still doing something wrong? The uuid
attribute is optional for the CoreData NSManagedObject, as well as for the domain item; but even it is not for the domain item, the error persists.
EDIT:
When explaining things, you always see things you did not see until then... I noticed that the "new" item was created with a plain init()
. I thought that possible this was the reason, as it should be initialized with proper values. So, I changed that to #2
let new: Country = .init(uuid: .init(), name: "")
and hoped for some change - but the fatal still appears...
from sworm.
Your code is correct. This error appears when keyPath is missing in attribute list (ManagedObjectConvertible conformance) - https://github.com/prisma-ai/Sworm/blob/main/docs/basic_usage.md
please, check your implementation.
from sworm.
Hm, checked it, but it seems to be as required - or do I miss something:
public struct Country: Codable, Equatable, IdentifiedItem {
public var uuid: UUID?
public var name: String
}
extension Country: ManagedObjectConvertible {
public init() {
self.uuid = .init()
self.name = .init()
}
public static let entityName = "Country"
public static let attributes: Set<Attribute<Country>> = [
.init(\.uuid, "uuid"),
.init(\.name, "name"),
]
public struct Relations {
let locations = ToManyRelation<Location>("locations")
}
public static let relations = Relations()
}
from sworm.
oh, i see - give me some time to investigate the problem, i will return to you soon
from sworm.
Great, thank you!
from sworm.
i'v realised that indirect keypath usage (via some protocol) doesn't work due to tricky swift runtime, so i suggest you solution implemented in pull request #3 ☝🏻
from sworm.
Well, looks good. Changed my code according to the changes in the pull request, and everything's working fine!
Thanks a lot for your support!
from sworm.
You are welcome 🤝
from sworm.
Related Issues (8)
- Is it possible to use Swarm as change tracker in CoreData? HOT 3
- Key path value type cannot be converted to contextual type HOT 1
- CoreData: warning: Unable to load class named '' for entity 'Entity'. Class not found, using default NSManagedObject instead. HOT 1
- Q: How to update an existing item? HOT 3
- Is it possible to sort items by several fields? HOT 2
- Cold you please add sample for "read / write data models or attributes separately." HOT 2
- Relations cannot be set HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sworm.