Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with custom NSManagedObject class core data in swift

I start to learn swift recently and I want to use some data model class written in Objective C. I got a weird bug when I try to save the data from input boxes:

Users.h

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>


@interface Users : NSManagedObject

@property (nonatomic, retain) NSString * password;
@property (nonatomic, retain) NSString * username;

- (NSString *)toString;
- (void)addFunny:(NSString *)prefix;

@end

Users.m

#import "Users.h"


@implementation Users

@dynamic password;
@dynamic username;

- (NSString *)toString
{
    return @"The username is \(username) and password is \(password)";
}

- (void)addFunny:(NSString *)prefix
{
    self.username = [NSString stringWithFormat:@"%@%@", prefix, self.username];
}

@end

Here is data model screenshot:

data model Here is the save function:

@IBAction func btnSave_Clicked(){
        println("Save \(txtUsername.text)")

        let appDel: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
        let context: NSManagedObjectContext = appDel.managedObjectContext
        let ent = NSEntityDescription.entityForName("Users", inManagedObjectContext: context)

        var newUser = Users(entity: ent, insertIntoManagedObjectContext: context)
        newUser.username = txtUsername.text
        newUser.password = txtPassword.text

        context.save(nil)
        println(newUser)

    }

Code from lldb:

Unresolved error Error Domain=NSCocoaErrorDomain Code=134100 "The operation couldn’t be completed. (Cocoa error 134100.)" UserInfo=0xb736c50 {metadata={
    NSPersistenceFrameworkVersion = 508;
    NSStoreModelVersionHashes =     {
        Entity = <b2bc8535 3bcfcdf1 81eecadc d32d8511 cc030525 d4eb7d76 94d11d7c f5853918>;
    };
    NSStoreModelVersionHashesVersion = 3;
    NSStoreModelVersionIdentifiers =     (
        ""
    );
    NSStoreType = SQLite;
    NSStoreUUID = "0169C569-2A57-47F1-9EF6-684485CB1135";
    "_NSAutoVacuumLevel" = 2;
}, reason=The model used to open the store is incompatible with the one used to create the store}, (error.userInfo)
like image 479
aladine Avatar asked Jun 23 '14 10:06

aladine


1 Answers

This has nothing to do with swift. If you update your coredata model without defining merging / versioning rules like mentioned in the Applee Doc, you need to delete and reinstall your application on the device or simulator.

Look at the error message:

The model used to open the store is incompatible with the one used to create the store}

Remove the app from the simulator/or device and perform a clean on your project. That should clear those issues up. Make sure that you are not running in the debugger when you delete the app or else it won't actually delete it properly.

If you want to be sure its gone (simulator), check this directory

Users/INSERT_YOUR_USER_HERE/Library/Application Support/iPhone Simulator/ for your app's folder, under the version you're running.

You can not expect that changing your already installed model will work on the device with no further effort.

like image 189
Alexander Avatar answered Oct 19 '22 21:10

Alexander