• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 在 restKit 0.20 中删除孤立对象

[复制链接]
菜鸟教程小白 发表于 2022-12-12 18:17:02 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

目前我正在努力删除孤立对象。 我有以下类(class)。

映射器类

在这个类中,我创建了我所有的 RKObjectManagers 并在我的其他类中使用它。

-(RKObjectManager *)mapAppointments{
    RKEntityMapping* appointmentMapping = [RKEntityMapping mappingForEntityForName"Appointment" inManagedObjectStore:managedObjectStore];
    appointmentMapping.identificationAttributes = @[@"app_id",@"app_start"] ;
    [appointmentMapping addAttributeMappingsFromDictionary{
                                                       @"AddressInfo": @"app_addressinfo",
                                                       @"Completed": @"app_completed",
                                                       @"Description": @"app_description",
                                                       @"EndDate""app_end",
                                                       @"FullDay": @"app_fullday",
                                                       @"Id""app_id",
                                                       @"Label": @"app_label",
                                                       @"LabelId": @"app_label_id",
                                                       @"Location": @"app_location",
                                                       @"rivate""app_private",
                                                       @"rojectName""app_project_name",
                                                       @"rojectNumber": @"app_project_number",
                                                       @"RecurrenceInfo": @"app_recurrenceInfo",
                                                       @"RelationAddressCity": @"app_relation_address_city",
                                                       @"RelationAddressId""app_relation_address_id",
                                                       @"RelationAddressName": @"app_relation_address_name",
                                                       @"RelationAddressStreet""app_relation_address_street",
                                                       @"RelationCode": @"app_relation_code",
                                                       @"RelationContactPersonId": @"app_relation_contact_id",
                                                       @"RelationContactPersonName": @"app_relation_contact_name",
                                                       @"RelationName""app_relation_name",
                                                       @"ReminderInfo""app_reminder_info",
                                                       @"StartDate": @"app_start",
                                                       @"State": @"app_state",
                                                       @"Subject": @"app_subject",
                                                       @"SupplierCode":@"app_supplier_code",
                                                       @"SupplierContactPersonId": @"app_supplier_contact_person_id",
                                                       @"SupplierContactPersonName":@"app_supplier_contact_person_name",
                                                       @"SupplierName": @"app_supplier_name",
                                                       @"Type": @"app_type",
                                                       @"ResxPers":@"app_resxPers",
                                                       }];







    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:appointmentMapping pathPattern:nil keyPath:@"" statusCodes:[NSIndexSet indexSetWithIndex:200]];
    NSArray *arrResponsDescriptor = [[NSArray alloc]initWithObjects:responseDescriptor,nil];

    [objectManager addResponseDescriptorsFromArray:arrResponsDescriptor];
    return objectManager;

}

网络服务类

在这个类中,我完成了我的请求,并尝试删除孤立对象。

-(void)fetchAppointmentsOnCompletionForNSDate *)start andEndNSDate *)end OnCompletionmyCompletion) compblock{
    Mapper *mapper = [Mapper new];
    RKManagedObjectStore *store = [[AdsolutDataModel sharedDataModel] objectStore];
    NSLog(@"store is %@",store);
    NSManagedObjectContext *context = store.mainQueueManagedObjectContext;
    RKObjectManager *objectManager = [mapper mapAppointments];

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setTimeZone:[NSTimeZone defaultTimeZone]];
    [dateFormatter  setDateFormat:@"dd-MM-yyyy"];
    NSString *strStart = [dateFormatter stringFromDate:start];
    NSString *strEnd = [dateFormatter stringFromDate:end];

    NSString *userName = [[NSUserDefaults standardUserDefaults]valueForKey:@"userName"];
    NSString *hash = [[NSUserDefaults standardUserDefaults]valueForKey:@"hash"];
    NSString *urlString = [NSString stringWithFormat:@"getcrmappointments?gebrcode=%@&token=%@&startdate=%@&enddate=%@",userName,hash,strStart,strEnd];


    [objectManager addFetchRequestBlock:^NSFetchRequest *(NSURL *URL) {

        RKPathMatcher *pathMatcher = [RKPathMatcher pathMatcherWithPattern:urlString];

        NSDictionary *argsDict = nil;
        BOOL match = [pathMatcher matchesPath:[URL relativePath] tokenizeQueryStrings:NO parsedArguments:&argsDict];

        if (match) {
            NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Appointment"];
            fetchRequest.predicate = [NSPredicate predicateWithFormat:@"(app_start >= %@) AND (app_end <= %@)", start,end]; // NOTE: Coerced from string to number
            fetchRequest.sortDescriptors = @[ [NSSortDescriptor sortDescriptorWithKey:@"app_id" ascending:YES] ];
            return fetchRequest;
        }

        return nil;
    }];

    NSURLRequest *request = [objectManager requestWithObject:nil method:RKRequestMethodGET path:urlString parameters:nil];
    RKManagedObjectRequestOperation *operation = [objectManager managedObjectRequestOperationWithRequest:request managedObjectContext:context success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {

        NSLog(@"REQUEST URL = %@",request.URL);
        NSError *error = nil;
        BOOL success = [context  save:&error];
        if (!success) RKLogWarning(@"Failed saving managed object context: %@", error);
        NSLog(@"SUCCESS");
        NSError *saveError = nil;

        for (Appointment *appointment in mappingResult.array) {
            NSLog(@"Appointment title is %@",appointment.app_subject);
            appointment.synchronized = @1;
            appointment.app_delete = @0;
            [context saveToPersistentStore:&saveError];

        }

        [self fetchAfwezighedenOnCompletionFor:start andEnd:end OnCompletion:^(BOOL finished) {
            if(finished){
                compblock(YES);
            }
        }];

    } failure:^(RKObjectRequestOperation *operation, NSError *error) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                        message:[error localizedDescription]
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
        NSLog(@"Hit error: %@", error);
    }];
    [objectManager enqueueObjectRequestOperationperation];
}

问题

目前,孤立对象并未被删除。我做错了什么还是设置不正确?

感谢您的帮助!



Best Answer-推荐答案


查看 RKObjectManager managedObjectRequestOperationWithRequest: 的代码表明,获取请求 block 是从传递给操作的对象管理器传递的。因此,需要进行调试才能弄清楚为什么没有调用它。它应该在所有“成功”场景(即响应描述符与接收到的响应匹配的场景)中调用。

您可以考虑使用管理器发出请求而不是获取请求并使用 RKManagedObjectRequestOperation,但理论上这应该只是更少的代码而不是功能上的差异。


尝试更改获取请求 block :

RKPathMatcher *pathMatcher = [RKPathMatcher pathMatcherWithPattern:@"getcrmappointments"];

关于ios - 在 restKit 0.20 中删除孤立对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21375981/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap