gpt4 book ai didi

iphone - 使用 RestKit 发布导致发布 nil 对象

转载 作者:可可西里 更新时间:2023-11-01 05:13:58 25 4
gpt4 key购买 nike

我在使用 RestKit 0.20pre6 发布 NSManagedObject 时遇到问题,它导致在我的 Rails 站点上创建一个值为 nil 的条目。我怀疑映射器,但据我所知,我已经正确设置了 RK 对象。例子很少,支持有限。该示例涉及 NSObjects 而不是 CoreData,因此我不得不做出一些假设。

这是我发布的代码,后面是控制台输出。该应用程序可以正常从服务器获取对象。

        // Sync the added object to the server
// ****************************************

RKObjecMapping *responseMapping = [RKObjectMapping mappingForClass:[Event class]];
// RKEntityMapping *responseMapping = [RKEntityMapping mappingForEntityForName:@"Event" inManagedObjectStore:self.managedObjectStore];
[responseMapping addAttributeMappingsFromDictionary:@{
@"id": @"eventId",
@"name": @"name",
@"amount": @"amount",
@"length": @"length",
@"updated_at": @"updatedAt",
@"created_at": @"createdAt",
@"latitude": @"latitude",
@"longitude": @"longitude",
@"thumbnail": @"thumbnail",
@"airTemp": @"airTemp",
}];

NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); // Anything in 2xx
RKResponseDescriptor *eventDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping
pathPattern:@"/events"
keyPath:@"events"
statusCodes:statusCodes
];

RKObjectMapping *requestMapping = [RKObjectMapping requestMapping]; // objectClass == NSMutableDictionary

[requestMapping addAttributeMappingsFromDictionary:@{
@"id": @"eventId",
@"name": @"name",
@"amount": @"amount",
@"length": @"length",
@"updated_at": @"updatedAt",
@"created_at": @"createdAt",
@"latitude": @"latitude",
@"longitude": @"longitude",
@"thumbnail": @"thumbnail",
@"airTemp": @"airTemp",
}];


// For any object of class Article, serialize into an NSMutableDictionary using the given mapping and nest
// under the 'article' key path
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping
objectClass:[Event class]
rootKeyPath:@"events"
];

LocationsAppDelegate *appDelegate = (LocationsAppDelegate *)[[UIApplication sharedApplication] delegate];
// RKObjectManager *manager = [RKObjectManager managerWithBaseURL:gBaseURL];


[RKObjectManager sharedManager].requestSerializationMIMEType = RKMIMETypeJSON;
[[RKObjectManager sharedManager] addRequestDescriptor:requestDescriptor];
[[RKObjectManager sharedManager] addResponseDescriptor:eventDescriptor];

NSLog(@"RVC-addEvent %@ - %@", appDelegate.user.login, appDelegate.user.password);
[[NBEventManager sharedManager] setUsername:appDelegate.user.login andPassword:appDelegate.user.password];
appDelegate.objectManager.HTTPClient = [NBEventManager sharedManager];


event = [eventsArray objectAtIndex:0];
[[RKObjectManager sharedManager] postObject:event
path:@"/events"
parameters:nil
success:nil
failure:nil
];

}

2013-01-15 23:25:52.857 fish[38803:13403] I restkit.network:RKHTTPRequestOperation.m:152 POST 'http://localhost:3000/events' 2013-01-15 23:25:52.969 fish[38803:16903] I restkit.network:RKHTTPRequestOperation.m:179 POST 'http://localhost:3000/events' (201 Created) [0.1121 s] 2013-01-15 23:25:52.971 fish[38803:17507] D restkit.object_mapping:RKMapperOperation.m:360 Executing mapping operation for representation: { event = { airTemp = ""; amount = ""; "created_at" = "2013-01-16T04:25:52Z"; id = 54; latitude = ""; length = ""; longitude = ""; name = ""; thumbnail = ""; "updated_at" = "2013-01-16T04:25:52Z"; "user_id" = ""; }; } and targetObject: (entity: Event; id: 0x9c7f710 ; data: { airTemp = 0; amount = nil; createdAt = "2013-01-16 04:25:52 +0000"; eventId = 0; latitude = "28.158466"; length = 0; longitude = "-82.509215"; name = nil; photo = nil; thumbnail = nil; updatedAt = nil; }) 2013-01-15 23:25:52.971 fish[38803:17507] D restkit.object_mapping:RKMapperOperation.m:287 Found mappable data at keyPath 'event': { airTemp = ""; amount = ""; "created_at" = "2013-01-16T04:25:52Z"; id = 54; latitude = ""; length = ""; longitude = ""; name = ""; thumbnail = ""; "updated_at" = "2013-01-16T04:25:52Z"; "user_id" = ""; } 2013-01-15 23:25:52.971 fish[38803:17507] D restkit.object_mapping:RKMapperOperation.m:232 Asked to map source object { airTemp = ""; amount = ""; "created_at" = "2013-01-16T04:25:52Z"; id = 54; latitude = ""; length = ""; longitude = ""; name = ""; thumbnail = ""; "updated_at" = "2013-01-16T04:25:52Z"; "user_id" = ""; } with mapping eventId>", " name>", " amount>", " length>", " updatedAt>", " createdAt>", " latitude>", " longitude>", " airTemp>", " thumbnail>" )> 2013-01-15 23:25:52.971 fish[38803:17507] D restkit.object_mapping:RKMappingOperation.m:754 Starting mapping operation... 2013-01-15 23:25:52.972 fish[38803:17507] D restkit.object_mapping:RKMappingOperation.m:818 Finished mapping operation successfully... 2013-01-15 23:25:52.973 fish[38803:17507] D restkit.object_mapping:RKMapperOperation.m:316 Found unmappable value at keyPath: events 2013-01-15 23:25:52.973 fish[38803:17507] D restkit.object_mapping:RKMapperOperation.m:389 Finished performing object mapping. Results: { event = " (entity: Event; id: 0x9c7f710 ; data: {\n airTemp = nil;\n amount = nil;\n createdAt = \"2013-01-16 04:25:52 +0000\";\n eventId = 54;\n latitude = nil;\n length = nil;\n longitude = nil;\n name = nil;\n photo = nil;\n thumbnail = nil;\n updatedAt = \"2013-01-16 04:25:52 +0000\";\n})"; } 2013-01-15 23:25:52.973 fish[38803:13403] D restkit.core_data:RKManagedObjectRequestOperation.m:534 Skipping deletion of orphaned objects: only performed for GET requests. 2013-01-15 23:25:52.976 fish[38803:11f03] D restkit.core_data:RKManagedObjectRequestOperation.m:599 Refreshing mapped target object (entity: Event; id: 0x9c7f710 ; data: { airTemp = nil; amount = nil; createdAt = "2013-01-16 04:25:52 +0000"; eventId = 54; latitude = nil; length = nil; longitude = nil; name = nil; photo = nil; thumbnail = nil; updatedAt = "2013-01-16 04:25:52 +0000"; }) in context

现在这是我的代码,因为我已将所有映射移至应用程序委托(delegate)并已实现 RKEntityMapping。我仍然没有 JSON 对象附加到 POST 调用。

    RKEntityMapping *eventMapping = [RKEntityMapping mappingForEntityForName:@"Event" inManagedObjectStore:managedObjectStore];
[eventMapping addAttributeMappingsFromDictionary:@{
@"id": @"eventId",
@"name": @"name",
@"amount": @"amount",
@"length": @"length",
@"updated_at": @"updatedAt",
@"created_at": @"createdAt",
@"latitude": @"latitude",
@"longitude": @"longitude",
@"thumbnail": @"thumbnail",
@"airTemp": @"airTemp",
}];
eventMapping.identificationAttributes = @[ @"eventId" ];

//RKRelationshipMapping *eventRelationship = [RKRelationshipMapping relationshipMappingFromKeyPath:@"events"
// toKeyPath:@"events"
// withMapping:eventMapping];
//[eventMapping addPropertyMapping:eventRelationship];

// Register our mappings with the provider
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:eventMapping
pathPattern:nil
keyPath:@"event"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:responseDescriptor];

RKEntityMapping *eventRequestMapping = [eventMapping inverseMapping];
RKRequestDescriptor *eventRequestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:eventRequestMapping
objectClass:[Event class]
rootKeyPath:@"event"
];
[objectManager addRequestDescriptor:eventRequestDescriptor];
[RKObjectManager sharedManager].requestSerializationMIMEType = RKMIMETypeJSON;

最佳答案

RESTKit 0.20pre6 中有一个错误,即使它是一个对象,它也会将 POST 请求的 HTTP Body 打印到 NSLog 为(null)。我在开发分支中修复了它。 https://github.com/RestKit/RestKit/pull/1139

关于iphone - 使用 RestKit 发布导致发布 nil 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14351531/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com