简体   繁体   English

NSManagedObject setValue forKey在线程中循环

[英]NSManagedObject setValue forKey looping in thread

I'm trying to save some data into database using CoreData so I created Entity named 'Client' with some attributes. 我试图使用CoreData将一些数据保存到数据库中,所以我创建了具有某些属性的名为“客户端”的实体。 Two of them are 'city' and 'post_code', both of String type. 其中两个是“ city”和“ post_code”,均为String类型。 I also created Client class extending NSManagedObjects and I wrote some methods there. 我还创建了扩展NSManagedObjects的Client类,并在那里写了一些方法。

-(void) setCity: (NSString*) city
{
    [self setValue:city forKey:@"city"];
}

-(NSString*) getCity
{
    return [self valueForKey:@"city"];
}

-(void) setPostCode: (NSString*) postCode
{
    [self setValue:postCode forKey:@"post_code"];
}

-(NSString*) getPostCode
{
    return [self valueForKey:@"post_code"];
}

getPostCode and setPostCode work as I expected but calling setCity or getCity is causing problems. getPostCode和setPostCode可以正常工作,但是调用setCity或getCity会引起问题。 Appication stops and method is looping in thread as you can see on screenshot. 如屏幕截图所示,停止应用程序并且方法在线程中循环。

在此处输入图片说明

Full size image 全尺寸图片

This is how I call those methods 这就是我所说的那些方法

 if([databaseResult count] > 0)
     c = [databaseResult objectAtIndex:0];
 else
     c = [NSEntityDescription insertNewObjectForEntityForName:@"Client" inManagedObjectContext:context];

[c setPostCode:[jsonData valueForKey:@"post_code_invoice"]];
[c setClientType:[jsonData valueForKey:@"company_type"]];
[c setCity:[jsonData valueForKey:@"city_invoice"]];

it always stops on setCity no matter what data I pass there, even that call doesn't work 无论我传递了什么数据,它总是在setCity上停止,即使该调用也不起作用

[c setCity:@"aaa"];

Did anyone had similar problem? 有人遇到过类似的问题吗?

The setValueForKey method is calling back into the same function because it has the same name. setValueForKey方法具有相同的名称,因此正在回调相同的函数。

From the KVC documentation 从KVC文档

Default Search Pattern for setValue:forKey: setValue:forKey的默认搜索模式:

When the default implementation of setValue:forKey: is invoked for a property the following search pattern is used: 为属性调用setValue:forKey:的默认实现时,将使用以下搜索模式:

The receiver's class is searched for an accessor method whose name matches the pattern set:. 在接收者的类中搜索名称与模式集合:相匹配的访问器方法。

If no accessor is found, and the receiver's class method accessInstanceVariablesDirectly returns YES, the receiver is searched for an instance variable whose name matches the pattern _, _is, , or is, in that order. 如果未找到访问器,并且接收方的类方法accessInstanceVariablesDirectly返回YES,则搜索接收方以其名称与_,_ is,或is模式匹配的实例变量。

If a matching accessor or instance variable is located, it is used to set the value. 如果找到匹配的访问器或实例变量,则将其用于设置值。 If necessary, the value is extracted from the object as described in “Representing Non-Object Values.” 如有必要,如“代表非对象值”中所述从对象中提取值。

If no appropriate accessor or instance variable is found, setValue:forUndefinedKey: is invoked for the receiver. 如果找不到合适的访问器或实例变量,则为接收者调用setValue:forUndefinedKey:。

So when you call setValue:forKey: with the key city , the implementation calls setCity: and your implementation calls setValue:forKey: , and round and round you go. 因此,当您使用键city调用setValue:forKey:时,实现将调用setCity:而您的实现将调用setValue:forKey: setCity:并进行四舍五入。

Why are you even doing it this way rather than setting the value directly? 为什么甚至用这种方式而不是直接设置值呢?

Or better still use properties and you don't even need to write setters or getters. 或者最好还是使用属性,而您甚至不需要编写setter或getter。

The other two methods work because the key names are different. 其他两种方法起作用,因为键名不同。 (they have underscores ) (有下划线)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM