我正在使用Core Data来保存一些字符串.我有以下类名为Results
Results.h
#import <CoreData/CoreData.h>
@interface Results : NSManagedObject
@property(nonatomic, retain) NSString *lessondate;
@property(nonatomic, retain) NSString *lesson;
@property(nonatomic, retain) NSString *location;
@property(nonatomic, retain) NSString *start;
@property(nonatomic, retain) NSString *end;
@end
Run Code Online (Sandbox Code Playgroud)
Results.m
#import "Results.h"
@implementation Results
@dynamic lessondate;
@dynamic lesson;
@dynamic location;
@dynamic start;
@dynamic end;
@end
Run Code Online (Sandbox Code Playgroud)
以下是我执行保存的代码:
-(void)saveLesson{
Results *result = (Results *)[NSEntityDescription insertNewObjectForEntityForName:@"Diary" inManagedObjectContext:managedObjectContext];
result.lessondate = calendarDateString;
result.lesson = [NSString stringWithFormat:@"%@", lessonText.text];
result.location = [NSString stringWithFormat:@"%@", locationTest.text];
result.start = [NSString stringWithFormat:@"%@", startTimeText.text];
result.end = [NSString stringWithFormat:@"%@", endTimeText.text];
NSError *error; …Run Code Online (Sandbox Code Playgroud) 我正在制作一个应用程序,您需要使用 4 位密码登录,但一次只能有一个密码。我正在尝试将其保存到核心数据,但每当用户添加新密码时,它只会将其添加到长列表中。如何限制一个实体只有一个它自己的实例?
这是我的代码,以防万一它会有所帮助:
-(BOOL)savePassword:(NSString*)password{
AppDelegate * appDelegate = [[AppDelegate alloc]init];
NSManagedObjectContext * context = [appDelegate managedObjectContext];
AppData * appData = (AppData*)[NSEntityDescription insertNewObjectForEntityForName:@"AppData" inManagedObjectContext:context];
appData.password = password;
NSError *error;
if (![context save:&error]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"AppData" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
if (fetchedObjects == nil) {
NSLog(@"There was an error:%@",error);
}
for (AppData * adata in fetchedObjects) {
NSLog(@"Password:%@",adata.password);
}
return …Run Code Online (Sandbox Code Playgroud) 如果我尝试将存储的属性添加到NSManagedObjectSwift 中的子类,而不为其提供默认值(请注意,我将在初始化程序中执行此操作),我会收到此错误:
Stored property 'foo' requires an initial value or should be @NSManaged
Run Code Online (Sandbox Code Playgroud)
代码如下:
class Thing : NSManagedObject{
var foo : String
var bar : String
init(foo: String, bar : String){
// blah, blah...
}
}
Run Code Online (Sandbox Code Playgroud)
强制执行的原因是什么?为什么我不能在初始化程序中初始化???
我需要能够从Core Data中获取对象并将它们保存在内存中的可变数组中,以避免不断获取和降低UI/UX.问题是我抓住了其他线程上的对象.我也有时在其他线程上写这些对象.因此,我不能只是保存NSManagedObjects在一个数组中,只是调用类似的东西,myManagedObjectContext.performBlock或者myObject.managedObjectContext.PerformBlock因为你不应该在线程之间传递MOC.
我正在考虑使用自定义对象将我需要的数据从CD对象中抛出.这感觉有点愚蠢,因为我已经为实体创建了Model/NSManagedObject类,并且因为自定义对象是可变的,所以它仍然不是线程安全的.这意味着我必须为多个线程上的对象操作执行类似串行队列的操作?因此,例如,每当我想要读/写/删除对象时,我必须将它扔到我的对象serialQueue中.
这一切看起来真的很讨厌所以我想知道这个问题有什么常见的设计模式或类似的东西吗?有没有更好的方法呢?
multithreading core-data grand-central-dispatch nsmanagedobject swift
对于以下代码,在下面两行之前的两个注释中,只有第一个有效.
NSManagedObject * Event = [NSEntityDescription insertNewObjectForEntityForName:str inManagedObjectContext:app.managedObjectContext];
//Work but strange
[Event setValue:[NSNumber numberWithInt:buf4[v+h]] forKey:value];
//Error
Event.value= [NSNumber numberWithInt:buf4[v+h]];
Run Code Online (Sandbox Code Playgroud)
第二次返回并出错
request for member 'value' in 'Event', which is of non-class type 'NSManagedObject*'
Run Code Online (Sandbox Code Playgroud) 我想更新Core Data中的对象.这是我的代码:
var detailTaskModel: TaskModel!
detailTaskModel.date = dateFromDP //update value
let appDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
appDelegate.saveContext()
Run Code Online (Sandbox Code Playgroud)
我正在使用NSFetchedResultsController和Swift.
更新:
var fetchedResultsController: NSFetchedResultsController = NSFetchedResultsController()
Run Code Online (Sandbox Code Playgroud)
我的func用于加载CoreData数据:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell: TaskCell = tableView.dequeueReusableCellWithIdentifier("myCell") as TaskCell
let thisTask = fetchedResultsController.objectAtIndexPath(indexPath) as TaskModel
cell.textLabel?.text = thisTask.task
return cell
}
Run Code Online (Sandbox Code Playgroud)
保存CoreData:
let appDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
let entityDescription = NSEntityDescription.entityForName("TaskModel", inManagedObjectContext: managedObjectContext!)
let task = TaskModel(entity: entityDescription!, insertIntoManagedObjectContext: managedObjectContext!)
task.task = labelText
Run Code Online (Sandbox Code Playgroud)