开发者

Setter failing selectively in NSManagedObject

I have a NSManagedObject which I'm trying to instantiate with given values. I access the setters like so:

object.couchID = (NSString *)[dictObject objectForKey:@"_id"];

...and this works fine on my machine, but my partner gets this error when he runs it on his machine:

'-[NSCFString type]: unrecognized selector sent to instance 0x4e465e0'

About 90% of the setters (all formatted in the same way) work on my partner's machine, but a good 10% fail with that error. All of them work on my machine.

We're running the exact same code (according to SVN (yes, I know)), and fetching the same data from the same server, so everything seems like it should work.

We've checked the objects being passed, and they're the same. Commenting out the setter allows the code to 开发者_运维问答get through to the next troublesome setter, but of course we need it to actually work. How else should we troubleshoot? Thanks in advance.


Update 1: Unlocked the Tumbleweed badge for that one... guess it's too sticky to touch? Any thoughts or guesses are welcome. And hey, you could earn 50 points.*


Update 2: the mixed-good-news is that checking out a fresh version from source control results in the same problem on my machine, so a) it's definitely something in the code, and b) I can more actively troubleshoot. Thanks for all your suggestions so far, I'm going to go through them all again.


I ran into something similar at work the other day. I suspect that one of you has a stale .momd file inside the app bundle, and that it's not being replaced when it gets upgraded. I suspect this is a bug in Xcode 4, though I haven't totally verified it yet. If your partner deletes the app completely and then installs the app, does the error go away?


You may need to create a temporary variable whose value is object cast to whatever the actual class is, e.g.

MyClass *c = (MyClass *)object; // if object is in fact a MyClass instance
c.couchID = (NSString *)[dictObject objectForKey:@"_id"];

I have seen cases where the compiler cannot make mental leap and realize that your attribute is the class you know it is. The solution for me in these cases has been to be more explicit. Does this make sense? It's worth a shot at least, no? :-)


if this code fails on your partner's machine:

someManagedObject.couchID = @"some hardcoded string";

seems like you have a dangling pointer: i would check that someManagedObject is properly retained and still a valid object when you try to call the -setCouchID method on it.


I have had nearly the same problem when trying to draw a CATiledLayer with data in NSManagedObjects. What should be a valid object barfs with an "unrecognised selector" exception

It nearly always happens because theres no retain on the object external to the point where you are trying to set or get the property. Being in a separate thread seemed to have a relationship too.

After fruitlessly trying to get round this with [NSManagedObjectContext lock] and retain on the context within the new thread I eventually just threw the contents of my fetch into a mutable set to try and keep a grip on it which seems to work on iOS but not on OS X so well.

So a couple of possibilities

  • Are you doing this not in the main thread and does the MOC have a retain within that thread. Check the docs for [NSManagedObjectContext lock]. But essentially each thread working with the context needs its own retain on the context.
  • Try throwing it into a container while you operate on it. Make it a bit stickier. Sorry if that sounds like voodoo but it is.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜