Core data 需要在restkit配置的应用程序中选择核心数据并发布
我还没有找到从现有的核心数据SQLite数据库中选择数据并发布的示例。根据调试输出,我的核心数据似乎配置正常。我已将其设置为显示SQLite操作。当我尝试对布尔属性执行谓词或加载时,它不会给出错误,但检索到零个对象Core data 需要在restkit配置的应用程序中选择核心数据并发布,core-data,post,boolean,restkit,predicate,Core Data,Post,Boolean,Restkit,Predicate,我还没有找到从现有的核心数据SQLite数据库中选择数据并发布的示例。根据调试输出,我的核心数据似乎配置正常。我已将其设置为显示SQLite操作。当我尝试对布尔属性执行谓词或加载时,它不会给出错误,但检索到零个对象 @interface Offering : NSManagedObject @property (nonatomic, retain) NSNumber * id; @property (nonatomic, retain) NSString * name; @property (
@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];
添加了以下内容并使其正常工作:
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]);
为什么我没有得到任何结果?为什么这不起作用?还有一个附带的问题:有人看到过从核心数据发布的好例子吗。我买了一本关于核心数据的书,我想我几乎找到了主要问题的答案:需要更多到托管对象上下文的链接。你是本地人,将核心数据代码与Restkit混合在一起,这让我觉得你做的事情不正常。下载RestKit并查看他们的RKTwitterCoreData示例。添加以下内容并将其添加到workNSFetchRequest*request=[[NSFetchRequest alloc]init];NSEntityDescription*实体=[NSEntityDescription entityForName:@OfferingInManagedObjectContext:u managedObjectContext];[请求集合实体:实体];n错误*anyError=nil;NSArray*fetched=[\uuu managedObjectContext executeFetchRequest:请求错误:&anyError];如果获取==nil{NSLog@Error使用fetch;}NSLog@We已获取%d个对象,[已获取计数];