Generic fetch #86

Closed
opened 2025-12-29 15:23:59 +01:00 by adam · 4 comments
Owner

Originally created by @lucasiscovici on GitHub (Sep 24, 2016).

Hey someone have an idea please ?
func fetch<T: NSManagedObject>(_ myconfig:String) -> [T] { return CoreStore.fetchAll(From<T>(myconfig)) }

Originally created by @lucasiscovici on GitHub (Sep 24, 2016). Hey someone have an idea please ? ` func fetch<T: NSManagedObject>(_ myconfig:String) -> [T] { return CoreStore.fetchAll(From<T>(myconfig)) } `
adam added the question label 2025-12-29 15:23:59 +01:00
adam closed this issue 2025-12-29 15:23:59 +01:00
Author
Owner

@JohnEstropia commented on GitHub (Sep 27, 2016):

Hi, thank you for the feedback!
I'm not sure I understand your question. Are you proposing to add your method to CoreStore?

If so, CoreStore intentionally avoids shared methods like this because fetches can come from either the DataStack (readonly)

CoreStore.fetchAll(From<T>(myConfig))

or any transaction (readwrite).

transaction.fetchAll(From<T>(myConfig))

so sorry, any utility that abstracts away the datastack and the transaction is highly unlikely to be added to CoreStore.

If I misunderstood your question, please clarify.

@JohnEstropia commented on GitHub (Sep 27, 2016): Hi, thank you for the feedback! I'm not sure I understand your question. Are you proposing to add your method to CoreStore? If so, CoreStore intentionally avoids shared methods like this because fetches can come from either the DataStack (readonly) ``` CoreStore.fetchAll(From<T>(myConfig)) ``` or any transaction (readwrite). ``` transaction.fetchAll(From<T>(myConfig)) ``` so sorry, any utility that abstracts away the datastack and the transaction is highly unlikely to be added to CoreStore. If I misunderstood your question, please clarify.
Author
Owner

@lucasiscovici commented on GitHub (Sep 27, 2016):

Thanks for you answer!
i want to create an abstract class CoreStoreHelper

`

 import Foundation
    import CoreStore

  public class CoreStoreHelper: NSObject {
    let myconf: String?
let string: AnyClass?

init(_ confe: String, type: AnyClass) {
    self.myconf=confe
    self.string = type

}
func addStorage(_ config:String){
    try! CoreStore.addStorageAndWait(
        SQLiteStore(
            fileName: filename_general,
            configuration: config,
            localStorageOptions: .recreateStoreOnModelMismatch
        )
    )
}
func fetch<T: NSManagedObject>() -> [T] {
    let f = From(self.string!,self.myconf)

    return CoreStore.fetchAll(f) as! [T] }

}
`

To help my NSManagedObject classes Helper
ex:

`

import Foundation

           import CoreStore
          import UIKit

          public class ImagesHelper: CoreStoreHelper {

let myconfig = "Images"
typealias myClass = Image

  init() {
    super.init(self.myconfig, type: myClass.self)

   addStorage(self.myconfig)

}


func transaction_Synchronous(_ id: Int64, data:NSData?,color:String?) {
    _ = CoreStore.beginSynchronous { (transaction) -> Void in
        let account1 = transaction.create(Into<myClass>(self.myconfig))
        account1.img_id=id
        account1.img_bin=data
        account1.color = color
        _ = transaction.commitAndWait()
    }


}

}
`
I succeed my generic fetch
i create From object

         func fetch<T: NSManagedObject>() -> [T] {
    let f = From(self.string!,self.myconf)

    return CoreStore.fetchAll(f) as! [T] }

`

@lucasiscovici commented on GitHub (Sep 27, 2016): Thanks for you answer! i want to create an abstract class CoreStoreHelper ` ``` import Foundation import CoreStore public class CoreStoreHelper: NSObject { let myconf: String? let string: AnyClass? init(_ confe: String, type: AnyClass) { self.myconf=confe self.string = type } func addStorage(_ config:String){ try! CoreStore.addStorageAndWait( SQLiteStore( fileName: filename_general, configuration: config, localStorageOptions: .recreateStoreOnModelMismatch ) ) } func fetch<T: NSManagedObject>() -> [T] { let f = From(self.string!,self.myconf) return CoreStore.fetchAll(f) as! [T] } ``` } ` To help my NSManagedObject classes Helper ex: ` import Foundation ``` import CoreStore import UIKit public class ImagesHelper: CoreStoreHelper { let myconfig = "Images" typealias myClass = Image init() { super.init(self.myconfig, type: myClass.self) addStorage(self.myconfig) } func transaction_Synchronous(_ id: Int64, data:NSData?,color:String?) { _ = CoreStore.beginSynchronous { (transaction) -> Void in let account1 = transaction.create(Into<myClass>(self.myconfig)) account1.img_id=id account1.img_bin=data account1.color = color _ = transaction.commitAndWait() } } ``` } ` I succeed my generic fetch i create From object ``` func fetch<T: NSManagedObject>() -> [T] { let f = From(self.string!,self.myconf) return CoreStore.fetchAll(f) as! [T] } ``` `
Author
Owner

@JohnEstropia commented on GitHub (Sep 28, 2016):

You should be able to create a generic From:

func fetch<T: NSManagedObject>() -> [T] {
    let f = From<R>(NSClassFromString(self.string!), self.myconf)
    return CoreStore.fetchAll(f)!
}

BUT, the compiler cannot guarantee that your self.string has any relation with <T: NSManagedObject>. You want to design a generic function but your input (var string) are dynamic.

If you want to make CoreStoreHelper fully generic, you will have to make it generic

class CoreStoreHelper<T: NSManagedObject> {
    // ...
    func fetch() -> [T] {
        // ...
    }
}
@JohnEstropia commented on GitHub (Sep 28, 2016): You should be able to create a generic `From`: ``` swift func fetch<T: NSManagedObject>() -> [T] { let f = From<R>(NSClassFromString(self.string!), self.myconf) return CoreStore.fetchAll(f)! } ``` BUT, the compiler cannot guarantee that your `self.string` has any relation with `<T: NSManagedObject>`. You want to design a generic function but your input (`var string`) are dynamic. If you want to make `CoreStoreHelper` fully generic, you will have to **make it generic** ``` class CoreStoreHelper<T: NSManagedObject> { // ... func fetch() -> [T] { // ... } } ```
Author
Owner

@JohnEstropia commented on GitHub (Sep 29, 2016):

I'll close this issue for now. If you have other questions feel free to continue the discussion :)

@JohnEstropia commented on GitHub (Sep 29, 2016): I'll close this issue for now. If you have other questions feel free to continue the discussion :)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#86