- objective-c - iOS 5 : Can you override UIAppearance customisations in specific classes?
- iphone - 如何将 CGFontRef 转换为 UIFont?
- ios - 以编程方式关闭标记的信息窗口 google maps iOS
- ios - Xcode 5 - 尝试验证存档时出现 "No application records were found"
非常简单的情况。不确定为什么会导致问题。
我有一个 View 在子 NSManagedObjectContext 中创建一个新的 NSManagedObject。当用户按下“完成”时,它保存子上下文,然后保存父上下文,然后它发布一个带有新创建对象的 objectID 的通知。在主视图 Controller 中,我响应通知并尝试使用 existingObjectWithID:error:
获取新创建的对象。
问题是这失败了,因为 objectID 是临时的(我收到“Cocoa 错误 133000”)。对两个上下文的保存是完美的:当我重新加载应用程序时,我可以看到我创建的条目。但是在我需要获取对新对象的引用时,它失败了。
为什么它在我已经保存它之后 给我一个临时对象 ID?
注意:我试过使用obtainPermanentIDsForObjects:error:
,它有效,但是当我尝试使用它获取对象时,永久ID仍然失败。
这是一些代码:
-(IBAction)done:(id)sender
{
if ([editorDoneNotification isEqualToString:kNOTIFICATION_OBJECTADDED]) {
// save the temporary moc
NSError* e;
if (![self.tempContext save:&e]) { // this is always a successful save
NSLog(@"Failed to save temporary managed object context: %@", [e localizedDescription]);
[[[UIAlertView alloc] initWithTitle:@"Database Error"
message:@"Failed to add object."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil] show];
}
}
NSError* e;
if (![[[AMDataModel sharedDataModel] mainContext] save:&e]) { // this is also successful
NSLog(@"Failed to save main managed object context: %@", [e localizedDescription]);
[[[UIAlertView alloc] initWithTitle:@"Database Error"
message:@"Failed to edit object."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil] show];
}
else
[[NSNotificationCenter defaultCenter] postNotificationName:editorDoneNotification object:[self.editingObject objectID]];
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
这就是我回复通知的方式:
-(void)objectAdded:(NSNotification*)notification
{
if (self.popoverController && [self.popoverController isPopoverVisible]) {
[self.popoverController dismissPopoverAnimated:YES];
}
NSManagedObjectID* newObjectID = (NSManagedObjectID*)(notification.object);
NSError* error;
AMObject* object = (AMObject*)[[[AMDataModel sharedDataModel] mainContext] existingObjectWithID:newObjectID error:&error]; // this is where the cocoa error 133000 happens
if (error != nil) {
NSLog(@"ERROR: Could not load new object in main managed object context.");
}
GMSMarker* m = [[GMSMarker alloc] init];
m.position = CLLocationCoordinate2DMake(object.latitudeValue, object.longitudeValue);
m.userData = object;
m.map = self.mapView;
[self.markers addObject:m];
}
-(void)objectEdited:(NSNotification *)notification
{
NSManagedObjectID* editedObjectID = (NSManagedObjectID*)notification.object;
NSError* error = nil;
AMObject* object = (AMObject*)[[[AMDataModel sharedDataModel] mainContext] existingObjectWithID:editedObjectID error:&error];
if (error != nil) {
NSLog(@"Error could not load edited object in main managed object context");
}
//update the UI based on edit
if ([self.popoverController isPopoverVisible]) {
[self.popoverController dismissPopoverAnimated:YES];
self.popoverController = nil;
}
}
最佳答案
因为 child 没有从父 MOC 得到更新。父 MOC 将使用永久 ID 更新其自己的 NSManagedObject
实例,但该更改不会下推到属于子 MOC 的 NSManagedObject
实例。
在这种情况下我不使用-objectID
。它有一些用途,但它不是永久性的 uniqueID。在这种情况下,我更愿意将我自己的 uniqueID 添加到实体,然后从主上下文中获取它。
您也可以只监听上下文保存通知或使用 NSFetchedResultsController 来接收更新。
关于ios - 新创建的 NSManagedObject 即使在保存后也会返回临时 objectID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18410456/
假设我在文件开头有这个定义: const ObjectId = mongoose.Types.ObjectId; 您更喜欢哪一个,为什么? // 1 new ObjectId; // 2 new O
我通过以下两种不同的方法生成了一个 ObjectId: user@ubuntu:~$ python Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24)
我编写了一个 Python 代码,用于从名为 Tweets 的 Mongo 集合中获取推文。我不想只获取对象文本并添加一个名为 Sentiment 的附加对象。 当我循环推文并将 json 对象解析为
我是 Node.js 和 mongodb 的新手。 在我的 Node.js 应用程序中,我使用的是 mongodb,对于 mongodb 操作,我使用的是 mongoose。 在 package.js
使用 parse.com 和 JavaScript SDK。 这就是我想要实现的目标。 页面上将用户( friend )列表返回给用户,然后他们可以单击其中一个用户,页面将更新以列出所有该用户的项目(
我有一个像这样的 objectId:["56153e4c2040efa61b4e267f","56033932efefe0d8657bbd9e"]为了在我的模型中保存该信息,我使用: items: [
我有一个标签模式: const TagSchema = new Schema({ label: {type: String, unique: true, minlength: 2}, upda
在online API他们指的是 Mongo::ObjectID。 我有 require 'mongo' 但 ruby 仍然找不到它。例如,我需要通过它的 Id 找到一个对象,我正在做: mong
作为管道的最后阶段,我有一个像这样的 $project : { ... anId : new ObjectId() } 但是 mongo 为每个文档生成相同的 Id。我希望它为每个投影文档生成
我需要在这样的数组中找到 mongoose objectID 的索引: [ { _id: 58676b0a27b3782b92066ab6, score: 0 }, { _id: 58676aca
我们可以从客户端生成 ObjectId 并在插入时使用它。我确实希望它在插入过程之外进行处理。我需要将其配置为我的默认 _id 生成过程,以便当我调用 insert 时,insert 方法应该创建自定
这是代码。跟随者和追随者无法工作。换句话说,我无法将像 {"user1", objectId1} 这样的对象推送到这两个数组。是否允许有一个字段同时包含对象 ID 和字符串名称?感谢您的帮助。 The
为什么 Meteor.js 使用它自己的 IDsp 算法> 为什么不使用 MongoDB ObjectId()? 最佳答案 如果你选择使用 Meteor,它对对象 ID 使用相同的方法: Meteor
Mongoose 菜鸟问题:我有两个 Mongoose ObjectId 对象列表,我将它们合并为一个。有些 ObjectId 是重复的,我不想将它们保存到我的数据库中。是否有 Mongoose 工具
以下代码从供应商集合中的员工数组中提取员工 await new VendorManager() .update( { emplo
我有一个简单的模型对象: class UserRating include MongoMapper::EmbeddedDocument key :idea_id, ObjectId key
谁能告诉我使用 #[objectId] 或 [id=objectId] 引用元素有什么区别? 最佳答案 第一个非常快,因为 jQuery 内部使用 getElementById当它识别出模式时(使用正
我的架构是: var schema = new Schema({ players: { type: [Schema.Types.ObjectId]}); 我正在使用 promi
这似乎是这样定义我的架构: var PossessionSchema = new mongoose.Schema({ thing: {type: mongoose.Schema.Types.Obj
我想要解决的是:使用建议的方法(mapReduce)保留 $in 中的 Id 数组的顺序: Does MongoDB's $in clause guarantee order 我已经完成了我的作业,发
我是一名优秀的程序员,十分优秀!