Coder Social home page Coder Social logo

Comments (9)

geor-kasapidi avatar geor-kasapidi commented on August 24, 2024

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.

innoreq avatar innoreq commented on August 24, 2024

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.

geor-kasapidi avatar geor-kasapidi commented on August 24, 2024

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.

innoreq avatar innoreq commented on August 24, 2024

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.

geor-kasapidi avatar geor-kasapidi commented on August 24, 2024

oh, i see - give me some time to investigate the problem, i will return to you soon

from sworm.

innoreq avatar innoreq commented on August 24, 2024

Great, thank you!

from sworm.

geor-kasapidi avatar geor-kasapidi commented on August 24, 2024

@innoreq please, look at #3

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.

innoreq avatar innoreq commented on August 24, 2024

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.

geor-kasapidi avatar geor-kasapidi commented on August 24, 2024

You are welcome 🤝

from sworm.

Related Issues (8)

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.