Bridge with objc project #326

Open
opened 2025-12-29 18:25:42 +01:00 by adam · 4 comments
Owner

Originally created by @paoloandrea on GitHub (Apr 22, 2020).

Hi, i've a project mixed swift/objc.
I using CoreStore in my Swift classes but I cannot understand how to mix the objc classes.
I need to import the CoreStore in my classes and after call the swift data stack make: fetch, save, delete.. etc
How to mix the project?
I know the information about the deprecate classes for objc, but my project is very big and i cannot transform all classes in swift.

Swift Stack code
`
@objc open
class Database:NSObject {
@objc public
//Stack for Objc
static let objcStack = CSDataStack.init(Database.stack)

 //Stack for Swift
static let stack : DataStack = {
    let stack = DataStack(xcodeModelName: "Model")
    try! stack.addStorageAndWait(
        SQLiteStore(
            fileName: "Model.sqlite",
            localStorageOptions: .recreateStoreOnModelMismatch
        )
    )
    return stack
}()

}
`

This is my objec-c code:
`
#import "TestController.h"
@import CoreStore; //<- ERROR --- needed call the CoreStore Classes? How to call it?

@implementation TestController

  • (void) viewDidLoad {
    [super viewDidLoad];
    [Database objcStack] // how to execute function like fetch, save, delete.. etc????
    //Do you have many example??
    }
    @end
    `

Thanks for help me.

Originally created by @paoloandrea on GitHub (Apr 22, 2020). Hi, i've a project mixed swift/objc. I using CoreStore in my Swift classes but I cannot understand how to mix the objc classes. I need to import the CoreStore in my classes and after call the swift data stack make: fetch, save, delete.. etc **How to mix the project?** I know the information about the deprecate classes for objc, but my project is very big and i cannot transform all classes in swift. Swift Stack code ` @objc open class Database:NSObject { @objc public //Stack for Objc static let objcStack = CSDataStack.init(Database.stack) //Stack for Swift static let stack : DataStack = { let stack = DataStack(xcodeModelName: "Model") try! stack.addStorageAndWait( SQLiteStore( fileName: "Model.sqlite", localStorageOptions: .recreateStoreOnModelMismatch ) ) return stack }() } ` This is my objec-c code: ` #import "TestController.h" @import CoreStore; //<- ERROR --- needed call the CoreStore Classes? How to call it? @implementation TestController - (void) viewDidLoad { [super viewDidLoad]; [Database objcStack] // how to execute function like fetch, save, delete.. etc???? //Do you have many example?? } @end ` Thanks for help me.
adam added the question label 2025-12-29 18:25:42 +01:00
Author
Owner

@JohnEstropia commented on GitHub (Apr 23, 2020):

See: https://github.com/JohnEstropia/CoreStore#objective-c-support

@JohnEstropia commented on GitHub (Apr 23, 2020): See: https://github.com/JohnEstropia/CoreStore#objective-c-support
Author
Owner

@paoloandrea commented on GitHub (Apr 23, 2020):

Hi JohnEstropia, thanks to your answer. :)
Can you write me if this process is ok?

MyPersonEntity is my NSManagedObject class

in the top class:
@import CoreStore;

in Class file
`[CSCoreStore beginAsynchronous:^(CSAsynchronousDataTransaction transaction) {
CSFrom
fromMyPersonEntity = CSFrom alloc] initWithEntityClass:[MyPersonEntity class;

        [transaction deletedObjectsOfType:fromMyPersonEntity];
        fromMyPersonEntity * newEntity = [transaction createInto:[[CSInto alloc] initWithEntityClass: fromMyPersonEntity]];
        
        [transaction
         commitWithSuccess:^{
            NSLog(@"Done");
            
         }
         failure: ^(CSError *error) {
            NSLog(@"Error: %@",error.localizedDescription);
         }];
    }];`

If this is not ok, can you explain me how to do this?


Another problem
In your example you can use CSFromClass like this:

CSFromClass([MyPersonEntity class])];

But in my code Xcode found a Mistake and doesn't compile.
To make the same work I need to make

[CSFrom alloc] initWithEntityClass:[MyPersonEntity class]];

How the Xcode can send this error? I need to import other CoreStore classes
Can I only use class allocation (CSFrom) to obtain the CSFromClass?

@paoloandrea commented on GitHub (Apr 23, 2020): Hi JohnEstropia, thanks to your answer. :) Can you write me if this process is ok? **MyPersonEntity** is my NSManagedObject class in the top class: `@import CoreStore;` in Class file `[CSCoreStore beginAsynchronous:^(CSAsynchronousDataTransaction *transaction) { CSFrom* fromMyPersonEntity = [[CSFrom alloc] initWithEntityClass:[MyPersonEntity class]]; [transaction deletedObjectsOfType:fromMyPersonEntity]; fromMyPersonEntity * newEntity = [transaction createInto:[[CSInto alloc] initWithEntityClass: fromMyPersonEntity]]; [transaction commitWithSuccess:^{ NSLog(@"Done"); } failure: ^(CSError *error) { NSLog(@"Error: %@",error.localizedDescription); }]; }];` If this is not ok, can you explain me how to do this? ---- Another problem In your example you can use CSFromClass like this: `CSFromClass([MyPersonEntity class])];` But in my code Xcode found a Mistake and doesn't compile. To make the same work I need to make `[CSFrom alloc] initWithEntityClass:[MyPersonEntity class]];` How the Xcode can send this error? I need to import other CoreStore classes Can I only use class allocation (CSFrom) to obtain the CSFromClass?
Author
Owner

@JohnEstropia commented on GitHub (Apr 24, 2020):

Your beginAsynchronous usage seems fine. Are you having problems with it?

As for CSFromClass not compiling, what errors are you getting?

@JohnEstropia commented on GitHub (Apr 24, 2020): Your `beginAsynchronous` usage seems fine. Are you having problems with it? As for `CSFromClass` not compiling, what errors are you getting?
Author
Owner

@paoloandrea commented on GitHub (Apr 24, 2020):

Hi John I've two different error:

  1. Implicit conversion of 'int' to 'Class _Nonnull' is disallowed with ARC
  2. Implicit declaration of function 'CSFromClass' is invalid in C99

And now I found another problem.
i start my database like your example:

@objc open
class Database:NSObject {
static let stack : DataStack = {
        let stack = DataStack(xcodeModelName: "MyDB")
        
        try! stack.addStorageAndWait(
            SQLiteStore(
                fileName: "MyDB.sqlite",
                localStorageOptions: .recreateStoreOnModelMismatch
            )
        )
        _ = try? stack.perform(
            synchronous: { (transaction) in
                // try transaction.deleteAll(From<Entity>())
        }
        )
        
        return stack
    }()
}

If I call the fetchOne or FetchAll in my other controller or NSObject class, to find the Entity the app crash

I use this function to fetch

Database.stack.fetchAll(From< Entity >())

I'm outside the main thread. ???

[CoreStore: Assertion Failure] DataStack+Querying.swift:146 fetchAll(::)
↪︎ Attempted to fetch from a 'CoreStore.DataStack' outside the main thread.

SourcePackages/checkouts/CoreStore/Sources/DataStack+Querying.swift, line 146

How can solve it???

Thank's for all your support.

@paoloandrea commented on GitHub (Apr 24, 2020): Hi John I've two different error: 1. Implicit conversion of 'int' to 'Class _Nonnull' is disallowed with ARC 2. Implicit declaration of function 'CSFromClass' is invalid in C99 And now I found another problem. i start my database like your example: ``` @objc open class Database:NSObject { static let stack : DataStack = { let stack = DataStack(xcodeModelName: "MyDB") try! stack.addStorageAndWait( SQLiteStore( fileName: "MyDB.sqlite", localStorageOptions: .recreateStoreOnModelMismatch ) ) _ = try? stack.perform( synchronous: { (transaction) in // try transaction.deleteAll(From<Entity>()) } ) return stack }() } ``` If I call the fetchOne or FetchAll in my other controller or NSObject class, to find the Entity the app crash I use this function to fetch `Database.stack.fetchAll(From< Entity >())` I'm outside the main thread. ??? > [CoreStore: Assertion Failure] DataStack+Querying.swift:146 fetchAll(_:_:) > ↪︎ Attempted to fetch from a 'CoreStore.DataStack' outside the main thread. > > SourcePackages/checkouts/CoreStore/Sources/DataStack+Querying.swift, line 146 **How can solve it???** Thank's for all your support.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore-JohnEstropia#326