NSFetchRequest返回包含<invalid> </invalid>类型元素的数组

时间:2014-09-17 18:30:08

标签: ios objective-c xcode core-data nsfetchrequest

我开始遇到一个我正在编写的核心数据应用程序的奇怪问题。我正在尝试使用一个简单谓词来执行NSFetchRequest,该谓词返回一个具有我期望的元素数量的数组,但每个元素的类型都是无效的。

此请求的谓词是字符串比较。我已经验证了Core Data实体在命令行中使用sqlite3设置了这个特定字段,并且我已经确认我传入谓词的字符串值是正确的。

为了安全起见,我甚至完全删除了谓词。当我这样做时,我得到一个数组,该数组的元素总数与我正在获取的核心数据实体的整个数据集相匹配,但是每个元素的类型都是无效的。

设置断点并检查无效元素后,我在控制台中得到了这个:

Printing description of [0]:
(<invalid>) [0] = <error: expected ']'
error: 1 errors parsing expression
>

在应用程序的另一部分中,我正在针对同一个实体执行类似的请求(没有谓词,只是获取整个集合)并且它正常工作。我对如何继续对此进行故障排除感到困惑,似乎无法在Google上找到有关获取请求返回这些结果的原因的任何信息。

以下是相关代码:

//appdelegate
- (NSManagedObjectContext *)managedObjectContext {
    if (_managedObjectContext != nil) {
        return _managedObjectContext;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil) {
        _managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType: NSMainQueueConcurrencyType];
        [_managedObjectContext setPersistentStoreCoordinator: coordinator];
    } 

    return _managedObjectContext;
}



//ClientDetailModel.m
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSError *error;
NSEntityDescription *ed = [NSEntityDescription entityForName:@"Clients" inManagedObjectContext: context];
NSFetchRequest *clientReq = [[NSFetchRequest alloc] init];
[clientReq setEntity: ed];

NSString *predicateString = [NSString stringWithFormat: @"%@\"%@\"", @"uuid == ", clientUuid];
[clientReq setPredicate: [NSPredicate predicateWithFormat: predicateString]];
NSArray *results = [context executeFetchRequest: clientReq error: &error];

客户核心数据模型:

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

@class Orders;

@interface Clients : NSManagedObject

@property (nonatomic, retain) NSNumber * account;
@property (nonatomic, retain) NSString * address;
@property (nonatomic, retain) NSString * city;
@property (nonatomic, retain) NSString * company;
@property (nonatomic, retain) NSString * contact;
@property (nonatomic, retain) NSString * email;
@property (nonatomic, retain) NSNumber * email1on;
@property (nonatomic, retain) NSString * email2;
@property (nonatomic, retain) NSNumber * email2on;
@property (nonatomic, retain) NSString * fax;
@property (nonatomic, retain) NSDate * last_synced;
@property (nonatomic, retain) NSDate * last_updated;
@property (nonatomic, retain) NSString * phone;
@property (nonatomic, retain) NSNumber * portal_id;
@property (nonatomic, retain) NSString * postal_code;
@property (nonatomic, retain) NSString * province;
@property (nonatomic, retain) NSNumber * sync_failures;
@property (nonatomic, retain) NSString * uuid;
@property (nonatomic, retain) NSString * client_hash;
@property (nonatomic, retain) NSSet *client_orders;
@end

@interface Clients (CoreDataGeneratedAccessors)

- (void)addClient_ordersObject:(Orders *)value;
- (void)removeClient_ordersObject:(Orders *)value;
- (void)addClient_orders:(NSSet *)values;
- (void)removeClient_orders:(NSSet *)values;

@end

1 个答案:

答案 0 :(得分:0)

事实证明,无效是从Core Data返回的对象的完全有效类型。事实上,只是将类型无效的对象强制转换为NSManagedObject解决了问题&#34;。

NSManagedObject *client = [results objectAtIndex: 0];

我很困惑,为什么数据类型在一切显然都不合适时会显示为无效,但我想我应该只是&#34;以不同的方式思考&#34;。