asked    Elma     2018-10-22       ios       84 view        2 Answers

[SOLVED] Realm model contains property but not found it at runtime

I am getting error as below

Invalid property name , reason: 'Property 'IsRecordDeleted' not found in object of type 'MyCustomModel'

Where as my Model is as Under

@objcMembers public class MyCustomModel : Object {
dynamic var   Id : String = ""
dynamic var   ProductId : String? = ""
dynamic var   IsRecordDeleted : Bool? = false
dynamic var   ProductBarcode : String? = ""


override public class func primaryKey() -> String? {
    return "Id"
}

}

and I am making query like this :

let mSavedItems = mDbHelper.realmObj.objects(MyCustomModel.self).filter("IsRecordDeleted = false")

What could be problem here. I do not know why my app is crashing with the same error. But If I change the value like

    let mSavedItems = mDbHelper.realmObj.objects(MyCustomModel.self).filter("ProductId = 0")

The app gets run, but crashed on when I use IsRecordDeleted in predicate.

Please tell me what could be problem

  2 Answers  

        answered    Winni     2018-10-22      

You can try

let mSavedItems = mDbHelper.realmObj.objects(MyCustomModel.self)
let filtered = mSavedItems.filter { $0.IsRecordDeleted == false }

and

let mSavedItems = mDbHelper.realmObj.objects(MyCustomModel.self) 
let filtered = mSavedItems.filter { $0.ProductId == "0" }

For both

let mSavedItems = mDbHelper.realmObj.objects(MyCustomModel.self) 
let filtered = mSavedItems.filter {
   $0.IsRecordDeleted == false 
   && $0.ProductId == "0" 
}

//

let resultPredicate = NSPredicate(format: "ProductId == '0' AND IsRecordDeleted == false")
let filtered = mSavedItems.filter(resultPredicate)


        answered    Gemma     2018-10-22      

I think you'll find that simply switching to filter blocks instead of string predicates might stop the crash, but will not produce the expected results.

This is because IsRecordDeleted never gets saved to the database. It is not a type that can be represented in Objective-C, therefore it cannot be dynamic, so Realm ignores it.

Take as an example the following class:

@objcMembers class MyObject: Object {
    dynamic var id = ""
    dynamic var testBool: Bool? = false

    override static func primaryKey() -> String {
        return "id"
    }
}

And say we initialize them like this:

    let obj1 = MyObject()
    obj1.id = "1"
    obj1.testBool = true

    let obj2 = MyObject()
    obj2.id = "2"
    obj2.testBool = false

    let realm = try? Realm()
    try? realm?.write {
        realm?.add(obj1, update: true)
        realm?.add(obj2, update: true)
    }

If we query Realm for these objects using realm.objects(MyObject.self), you'll get something like this

Results<MyObject> <0x7fe410c0ad90> (
    [0] MyObject {
        id = 1;
    },
    [1] MyObject {
        id = 2;
    }
)

And you'll see that in the database, there indeed is no property named testBool, which was our optional Bool.

You can easily see that the optional Bool may cause problems if you write it out like this instead:

class MyObject: Object {
    @objc dynamic var id = ""
    @objc dynamic var testBool: Bool? = false   // This line will not compile.

    override static func primaryKey() -> String {
        return "id"
    }
}

I'm curious why the IsRecordDeleted needs to be optional in the first place, since it seems to have a value from the get-go. If it doesn't, then something like this will work as expected:

@objcMembers public class MyCustomModel: Object {
    dynamic var Id: String = ""
    dynamic var ProductId: String? = ""
    dynamic var IsRecordDeleted: Bool = false
    dynamic var ProductBarcode: String? = ""


    override public class func primaryKey() -> String? {
        return "Id"
    }
}

and you can query via string like you were trying to do in the first place.

If it has to be optional, then Realm provides a RealmOptional for this exact case, that you can look into here.





Your Answer





 2018-10-22         Wilbur

send token to server in angular universal

I have an Angular 6+ app which is configured to utilize server side rendering using Angular Universal.I also used TransferState to avoid duplicate API calls on the server and client app.Authentication in my angular app is based on token.The problem is in the first time the user opens my web app, which results to rendering index.html for a user which is not authenticated whereas the user actually is logged in but there is no opportunity to transfer the token to the server. So, when the client app swapped with server app, it is required to call APIs again because of the exist...
 node.js                     1 answers                     60 view
 2018-10-22         Geoffrey

Kubernetes TLS issue

Im having some issues when building my Kubernetes Cluster.Whenever I try to run a kubectl command, or my ansible scrips does, it gets the following error: Unable to connect to the server: tls: either ServerName or InsecureSkipVerify must be specified in the tls.ConfigNot from a script or by hand can anything execute the kubectl command, yet the apiserver pod is running.Any help will be greatly appreciated. You probably either haven't specified the server name or the CA certificate, or specified an incorrect one. Make sure you have specified the correct CA and server ...
 kubernetes                     1 answers                     62 view
 2018-10-22         Nicholas

Is there any way to get namespaces based on a particular metadata label

My namespace has some custom metadata labels. Some have the labels some don't. Is there any way to get the namespaces which has a particular label using kubectl? Yes. Like so:$ kubectl create ns nswithlabels$ kubectl label namespace nswithlabels this=thing$ kubectl describe ns/nswithlabelsName: nswithlabelsLabels: this=thingAnnotations: <none>Status: ActiveNo resource quota.No resource limits.$ kubectl get ns -l=thisNAME STATUS AGEnswithlabels Active 6mNote: I could have also used -l=this=thing in the last command to specify b...
 kubernetes                     1 answers                     64 view