实例化NSManagedObject而不保存它
我得到了一个类:
@objc(User) class User: NSManagedObject { @NSManaged var id: String //MARK: - Initialize convenience init(id: String, context: NSManagedObjectContext?) { // Create the NSEntityDescription let entity = NSEntityDescription.entityForName("User", inManagedObjectContext: context!) // Super init the top init self.init(entity: entity!, insertIntoManagedObjectContext: context) // Init class variables self.id = id } }
我在ViewController中创建了一个新用户:
managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext! var aUser = User(id: "500T", context: managedObjectContext)
我有另一个类“Group”,其中Group有很多用户。 为了节省用户,我做了类似的事情
aGroup!.users = NSSet(array: users!) !managedObjectContext.save
我不想保存所有用户。 如何在不保存的情况下实例化用户?
我找到了答案,并向用户init添加了一个needSave布尔值。 也许这不是最好的答案,但它正在发挥作用。
@objc(User) class User: NSManagedObject { @NSManaged var id: String //MARK: - Initialize convenience init(id: String, needSave: Bool, context: NSManagedObjectContext?) { // Create the NSEntityDescription let entity = NSEntityDescription.entityForName("User", inManagedObjectContext: context!) if(!needSave) { self.init(entity: entity!, insertIntoManagedObjectContext: nil) } else { self.init(entity: entity!, insertIntoManagedObjectContext: context) } // Init class variables self.id = id } }
- 我是否应该在一个对象中使用@property(nonatomic,copy)或@property(nonatomic,strong)作为我的(NSString *)attr?
- 应用的audio无法通过扬声器工作
- 在X轴coreplot上使用Epoch时间
- string格式的date转换为stringdate前一天的nsdate结果
- 为设备和模拟器创build32位和64位静态库 – Xcode
- iOS Autolayout如何水平拉伸2个button
- Swift中ARC不可用的方法
- UITextView scrollEnabled = YES在iOS8中设置scrollEnabled = NO后不工作
- 在iOS体系结构中使用合成