从数据库NSManagedObject

时间:2015-12-11 07:51:26

标签: ios core-data

我正在开发iOS应用程序,它从Web服务获取数据并将其保存到本地数据库并根据需要使用各种视图。数据从Web Service获取并正确保存到数据库。但是,在访问来自NSManagedObject的数据时,它会显示FAULT,而后续NSDictionary会显示null

代码如下: -

-(void)SaveData:(NSInteger)menuId categoryID:(NSInteger)catID   caption:(NSString *)title parentID:(NSInteger)parentID
{
    BBAppDelegate *appDelegate = [[BBAppDelegate alloc]init];

    NSManagedObjectContext *context = [appDelegate managedObjectContext];

    NSManagedObject *userobj = [NSEntityDescription insertNewObjectForEntityForName:tableName
                                                             inManagedObjectContext:context];
    [userobj setValue:[NSNumber numberWithInt:menuId] forKey:@"id"];

    [userobj setValue:[NSNumber numberWithInt:catID] forKey:@"catid"];

    [userobj setValue:[NSNumber numberWithInt:parentID] forKey:@"parentid"];

    [userobj setValue:title forKey:@"caption"];

    NSError *error = nil;
    // Save the object to persistent store
    if (![context save:&error]) {
        NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
    }

    NSLog(@"app dir: %@",[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]);
}
-(NSArray *)fetchedData{

    BBAppDelegate *appdelegate = [[BBAppDelegate alloc]init];

    NSManagedObjectContext *context = [appdelegate managedObjectContext];

    NSFetchRequest *readData = [[NSFetchRequest alloc] init];

    [readData setReturnsObjectsAsFaults:NO];


    NSEntityDescription *entity = [NSEntityDescription
                                   entityForName:tableName inManagedObjectContext:context];

    [readData setEntity:entity];

    NSError *error = nil;

    NSArray *results = [context executeFetchRequest:readData error:&error];

//    for (Test *test in array)
//    {
//        NSLog(@"Test: %@ ", test.text);
//    }

    NSLog(@"%@",results);

    return results;

}
-(void)removeData{

    BBAppDelegate *appdelegate = [[BBAppDelegate alloc]init];

    NSManagedObjectContext *context = [appdelegate managedObjectContext];

    NSFetchRequest *delData = [[NSFetchRequest alloc]init];

    [delData setEntity:[NSEntityDescription entityForName:tableName inManagedObjectContext:context]];

    [delData setIncludesPropertyValues:NO];

    NSError *error = nil;

    NSArray *result = [context executeFetchRequest:delData error:&error];

    for (NSManagedObject *value  in result) {
        [context deleteObject:value];
    }
    NSError *saveError = nil;

    [context save:&saveError];
}
-(void)table:(NSString *)nameTable{

    tableName =nameTable;
}
- (instancetype)initWithTableName:(NSString *)tablename
{
    [self table:tablename];

    return self;
}
-(NSMutableArray *)buldMenu{

    NSArray *array = [self fetchedData];

    return [self getmenus:0 arr:array];
}
-(NSMutableArray *)getmenus:(NSInteger)parentID arr: (NSArray *)array
{
    NSMutableArray *aray = [[NSMutableArray alloc]init];

    for (NSManagedObject *dic in array) {

        if (dic.faultingState != 0) {

            NSLog(@"%lu",(unsigned long)dic.faultingState);

            //return nil;
        }

        NSArray *keys = [[[dic entity] attributesByName] allKeys];

        NSLog(@"%@",keys);

        NSDictionary *dict = [dic dictionaryWithValuesForKeys:keys];
        NSLog(@"%@",dic);

        NSLog(@"%@",dict);


        NSInteger pID = [[dict  objectForKey:@"parentid"]integerValue];

        if (pID == parentID) {
            BBMenuEntry *entryMenu = [[BBMenuEntry alloc]init];
            entryMenu.menuID = [[dict objectForKey:@"id"]integerValue];
            entryMenu.catID = [[dict objectForKey:@"catid"]integerValue];
            entryMenu.caption = [dict objectForKey:@"caption"];
            entryMenu.subMenus = [self getmenus:entryMenu.menuID arr:array];

            [aray addObject:entryMenu];

        }
    }
    return aray;
}

0 个答案:

没有答案