0

我无法找到从现有 Core Data SQLite DB 中选择数据并发布它的示例。根据调试输出,我的核心数据似乎配置正常。我已将其设置为显示 SQLite 操作。当我尝试对布尔属性执行谓词或加载时,它不会给出错误,但它会检索零个对象。

@interface Offering : NSManagedObject

@property (nonatomic, retain) NSNumber * id;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSNumber * uploadToServerRequired; 



#import "AppDelegate.h"
#import <RestKit/RestKit.h>
#import "Offering.h"

@implementation AppDelegate

@synthesize window = _window;
@synthesize managedObjectContext = __managedObjectContext;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions


    RKObjectManager* manager = 
    [RKObjectManager objectManagerWithBaseURL: [[NSURL  alloc] initWithString:@"http://localhost:8080/anywhereAboutWebServices"] ];
    manager.objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:@"AnywhereAbout2.sqlite"];

 NSError *error;

    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"AnywhereAbout2.sqlite"];
    NSURL *url = [NSURL fileURLWithPath:path];

    NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
    NSPersistentStoreCoordinator *persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel];
    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:[NSDictionary dictionaryWithObjectsAndKeys:     [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,                                 [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil] error:&error]);
    else {
        __managedObjectContext = [[NSManagedObjectContext alloc] init];
        [__managedObjectContext setPersistentStoreCoordinator: persistentStoreCoordinator];

    }

    NSArray* objects = [Offering allObjects];
    NSLog(@"We loaded %d objects", [objects count]);

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"uploadToServerRequired == %@", [NSNumber numberWithBool: (BOOL)1]];


    int count = [filtered count];
4

1 回答 1

0

添加以下内容并使其工作:

NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Offering" inManagedObjectContext:__managedObjectContext];
[request setEntity:entity];

NSError *anyError = nil;
NSArray *fetched = [__managedObjectContext executeFetchRequest:request error: &anyError];
if (fetched == nil) {
     NSLog(@"Error with fetch");
}
NSLog(@"We fetched %d objects", [fetched count]);
于 2012-07-17T15:02:34.597 回答