我是Grand Central Dispatch(GCD)和Core Data的初学者,我需要你的帮助来使用带有CGD的Core Data,这样当我向Core Data添加40.000条记录时,UI不会被锁定.
我知道CD不是线程安全的,所以我必须使用另一个上下文,然后保存数据和合并上下文,就我从一些文章中能够理解的那样.
我还做不到的是把各个部分放在一起.
所以,在我的代码中,我需要你的帮助,如何做到这一点.
我有:
/*some other code*/
for (NSDictionary *memberData in arrayWithResult) {
//get the Activities for this member
NSArray *arrayWithMemberActivities = [activitiesDict objectForKey:[memberData objectForKey:@"MemberID"]];
//create the Member, with the NSSet of Activities
[Members createMemberWithDataFromServer:memberData
andActivitiesArray:arrayWithMemberActivities
andStaffArray:nil
andContactsArray:nil
inManagedObjectContext:self.managedObjectContext];
}
Run Code Online (Sandbox Code Playgroud)
如何将其转换为在后台工作,然后,在完成保存后,保存数据并更新UI,而不会在保存40.000对象时阻止UI?
我的应用程序使用核心数据(在Magical Record的帮助下)并使用相当多的多线程NSOperation.
当然,我非常小心,只能NSManagedObjectID在线程/操作之间传递.
现在,为了回到操作中相应的托管对象,我使用-existingObjectWithID:error::
Collection *owner = (Collection *)[localContext existingObjectWithID:self.containerId error:&error];
Run Code Online (Sandbox Code Playgroud)
但我得到的是零,并error说这是一个错误#13300 : NSManagedObjectReferentialIntegrityError.
以下是文档中有关此错误的内容:
NSManagedObjectReferentialIntegrityError
Error code to denote an attempt to fire a fault pointing to an object that does not exist.
The store is accessible, but the object corresponding to the fault cannot be found.
Run Code Online (Sandbox Code Playgroud)
在我的情况下,这不是真的:该对象存在.实际上,如果我用a迭代遍历该Collection实体的所有实例NSFetchRequest,我会在其中找到它,它NSManagedObjectID就是我传递给它的那个实体-existingObjectWithID:error:.
而且,如果我使用-objectWithID:,我会得到一个正确的对象.
所以我有些东西不见了.以下是一些其他观察/问题:
所以也许我错过了什么existingObjectWithID:error:呢?文件说:
If there is …Run Code Online (Sandbox Code Playgroud) 所以,我的情况是这样的:
我的iOS应用程序中有一个NSManagedObject子类,作为一个属性,我想存储MKPolygon对象的内容.我决定解决这个问题的方式(以及它是否有效可能是一个不同的问题)是将polygon属性声明为可转换对象,然后存储包含多边形点的NSArray(作为NSValue对象).
为此,我在模型对象上编写了几个便捷类方法:
+ (NSArray *)coordsArrayFromMKPolygon:(MKPolygon *)polygon pointCount:(int)count
{
CLLocationCoordinate2D *coords = (CLLocationCoordinate2D *)malloc(sizeof(CLLocationCoordinate2D) * count);
[polygon getCoordinates:coords range:NSMakeRange(0, count)];
NSMutableArray *coordsArray = [NSMutableArray array];
for (int i = 0; i < count; i++) {
NSValue *coordVal = [NSValue valueWithBytes:&coords[i] objCType:@encode(CLLocationCoordinate2D)];
[coordsArray addObject:coordVal];
}
free(coords);
return [NSArray arrayWithArray:coordsArray];
}
+ (MKPolygon *)polygonFromCoordsArray:(NSArray *)coordsArray pointCount:(int)count
{
CLLocationCoordinate2D *coords = (CLLocationCoordinate2D *)malloc(sizeof(CLLocationCoordinate2D) * count);
for (int i = 0; i < count; i++) {
CLLocationCoordinate2D coord;
[[coordsArray objectAtIndex:i] getValue:&coord];
coords[i] = coord; …Run Code Online (Sandbox Code Playgroud) 保存到核心数据时偶尔会出现此错误.我无法重新创建它.
有没有人有这个错误的经验;
CoreData: error: NULL _cd_rawData but the object is not being turned into a fault
Run Code Online (Sandbox Code Playgroud)
?
看这个高低,但找不到我的答案.我期待查询所有不等于指定字符串的记录的核心数据.例如,所有记录不等于当前会话ID.我试过这些无济于事:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"listingID != %@", [sitListingID objectAtIndex:i]];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%@ NOT CONTAINS[cd] %@",@"listingID", [sitListingID objectAtIndex:i]];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%@ NOT CONTAINS %@",@"listingID", [sitListingID objectAtIndex:i]];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"listingID NOT like %@", [sitListingID objectAtIndex:i]];
Run Code Online (Sandbox Code Playgroud)
没什么用.HEEEEELP !!! -------------------------------------------------- ---更多代码
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"ListingRecord" inManagedObjectContext:context];
[request setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"sessionID <> %@", uniqueSessionListings];
[request setPredicate:predicate];
NSError *error = nil;
NSMutableArray *mutableFetchResults = [[context executeFetchRequest:request error:&error] mutableCopy];
Run Code Online (Sandbox Code Playgroud) 我一直在尝试添加核心数据.每次我得到同样的错误:
error: filename "EntityName +CoreDataClass.swift" used twice: '/Users/userName/Desktop/Development/MyApp/AppName/EntityName +CoreDataClass.swift' and '/Users/userName/Library/Developer/Xcode/DerivedData/AppName-dgwzrmxsetzvtedibxrazuutjwnh/Build/Intermediates/AppName.build/Debug-iphoneos/AppName.build/DerivedSources/CoreDataGenerated/Model/EntityName +CoreDataClass.swift'
Run Code Online (Sandbox Code Playgroud)
我使用以下步骤添加核心数据:
1.New file/DataModel; 将它保存在我的项目的根目录中,
选择Model.xcdatamodeld并添加实体,添加几个属性,保存,编辑/创建NSManagedObjectClass子类.
因此,我在导航器中观察到4个新文件:Model.xcdatamodeld,EntityName + CoreDataProperties.swift,EntityName + CoreDataClass.swift,_COREDATA_DATAMODELNAME_ + CoreDataModel.swift
他们的内容: _COREDATA_DATAMODELNAME_ + CoreDataModel.swift:
import Foundation
import CoreData
___COREDATA_DATAMODEL_MANAGEDOBJECTCLASSES_IMPLEMENTATIONS___
Run Code Online (Sandbox Code Playgroud)
EntityName + CoreDataClass.swift:
import Foundation
import CoreData
class EntityName: NSManagedObject {
}
Run Code Online (Sandbox Code Playgroud)
实体名称+ CoreDataProperties.swift:
import Foundation
import CoreData
extension EntityName {
@nonobjc class func fetchRequest() -> NSFetchRequest< EntityName > {
return NSFetchRequest< EntityName >(entityName: "EntityName");
}
@NSManaged var str: String?
}
Run Code Online (Sandbox Code Playgroud)
我尝试过:
1.清理构建,删除DerivedData,删除var /文件夹的内容,重启
2.删除生成的文件,在导航器中显示
我所有的努力都没有运气. …
我正在编写一个需要加密数据的iPhone应用程序.我已经学会了如何通过设置NSFileProtectionComplete属性来打开文件加密.我也知道如何检查iPhone版本以确保它们运行的是iOS 4.0或更高版本.
我已经意识到,如果用户没有选择密码并且没有在设置>常规>密码锁屏幕上专门启用数据保护,那么数据实际上根本不受保护.
我想弹出警告并告诉用户他们必须启用密码并启用数据保护(这需要在4前iPhone上进行备份和恢复),然后如果他们没有密码则退出应用程序并启用数据保护.我无论如何都无法弄清楚这些设置的状态.我发现的所有API,例如UIApplication中的"protectedDataAvailable",如果禁用数据保护,都会成功通过.
我将开始更新这个,以帮助那些寻求使用它作为他们自己的个人代码的参考.
最新更新
另一个有助于正确设置的补充
@property (nonatomic, readwrite) BOOL unlocked;到AppDelegate.h
和@synthesize unlocked;到AppDelegate.m.然后我改变了我的-
(NSPersistentStoreCoordinator *)persistentStoreCoordinator方法以及我的- (void)mergeChangesFrom_iCloud方法,两者都将在下面显示(在持久存储设置的中间和iCloud合并方法的底部).本质上,我告诉应用程序阻止iCloud合并数据,直到应用程序设置其持久存储.否则,您将看到应用程序因无法读取的故障而崩溃.以下是我设置persistentStoreCoordinator的方法:
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (__persistentStoreCoordinator != nil)
{
return __persistentStoreCoordinator;
}
// here is where you declare the persistent store is not prepared;
self.unlocked = NO;
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Maintain_My_Car.sqlite"];
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
NSPersistentStoreCoordinator *psc = __persistentStoreCoordinator;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSDictionary *options = …Run Code Online (Sandbox Code Playgroud) 我对CoreData有一点该死的问题.我想插入一个新的Object,所以我首先要创建一个.这是由该代码完成的:
Challenges *newChallenge = [[Challenges alloc] init];
[newChallenge setName:@"TestChallenge"];
[newChallenge setRounds:[[NSNumber alloc] initWithInt:12]];
[newChallenge setShots:[[NSNumber alloc] initWithInt:5]];
[newChallenge setDate:[NSDate date]];
Run Code Online (Sandbox Code Playgroud)
但是在alloc init之后我得到了这个错误:
CoreData: error: Failed to call designated initializer on NSManagedObject class 'Challenges'
Run Code Online (Sandbox Code Playgroud)
黑客出了什么问题?
core-data ×10
iphone ×5
ios ×4
cocoa ×1
encryption ×1
fault ×1
icloud ×1
initializer ×1
ios4 ×1
ios5 ×1
nspredicate ×1
objective-c ×1
swift ×1
xcode8 ×1