标签: core-data

核心数据和线程/ Grand Central Dispatch

我是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?

iphone multithreading core-data grand-central-dispatch ios

40
推荐指数
2
解决办法
1万
查看次数

Core Data -existingObjectWithID:error:导致错误133000

我的应用程序使用核心数据(在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:,我会得到一个正确的对象.

所以我有些东西不见了.以下是一些其他观察/问题:

  • "一个不存在的对象":那句话中"存在"的含义是什么?"存在"在哪里?那时它在我的Core Data商店中肯定"存在".
  • "无法找到与故障对应的对象":该句中"找到"的含义是什么?"发现"在哪里?在那时我肯定会在我的Core Data商店中找到它.

所以也许我错过了什么existingObjectWithID:error:呢?文件说:

If there is …
Run Code Online (Sandbox Code Playgroud)

cocoa core-data fault

40
推荐指数
2
解决办法
8849
查看次数

如何仅获取Core Data中的第一条记录?

我正在使用Core Data,我只想获取数据集中的第一条记录,是否可能?

iphone core-data

39
推荐指数
2
解决办法
3万
查看次数

是否可以在NSManagedObject子类中覆盖@dynamic属性的getter和setter?

所以,我的情况是这样的:

我的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)

core-data nsmanagedobject ios

39
推荐指数
1
解决办法
1万
查看次数

CoreData:错误:NULL _cd_rawData但对象没有变成错误

保存到核心数据时偶尔会出现此错误.我无法重新创建它.

有没有人有这个错误的经验;

CoreData: error: NULL _cd_rawData but the object is not being turned into a fault
Run Code Online (Sandbox Code Playgroud)

iphone core-data

39
推荐指数
2
解决办法
1万
查看次数

NSPredicate查询不包含特定字符串

看这个高低,但找不到我的答案.我期待查询所有不等于指定字符串的记录的核心数据.例如,所有记录不等于当前会话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)

core-data nspredicate ios

39
推荐指数
1
解决办法
3万
查看次数

Xcode-beta 8.无法创建核心数据

我一直在尝试添加核心数据.每次我得到同样的错误:

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.删除生成的文件,在导航器中显示

我所有的努力都没有运气. …

core-data ios swift xcode8

39
推荐指数
4
解决办法
2万
查看次数

如何确定iPhone用户当前是否设置了密码并启用加密?

我正在编写一个需要加密数据的iPhone应用程序.我已经学会了如何通过设置NSFileProtectionComplete属性来打开文件加密.我也知道如何检查iPhone版本以确保它们运行的​​是iOS 4.0或更高版本.

我已经意识到,如果用户没有选择密码并且没有在设置>常规>密码锁屏幕上专门启用数据保护,那么数据实际上根本不受保护.

我想弹出警告并告诉用户他们必须启用密码并启用数据保护(这需要在4前iPhone上进行备份和恢复),然后如果他们没有密码则退出应用程序并启用数据保护.我无论如何都无法弄清楚这些设置的状态.我发现的所有API,例如UIApplication中的"protectedDataAvailable",如果禁用数据保护,都会成功通过.

iphone encryption data-protection core-data ios4

38
推荐指数
2
解决办法
9770
查看次数

在app delegate中添加持久存储(启用iCloud)时崩溃

我将开始更新这个,以帮助那些寻求使用它作为他们自己的个人代码的参考.

最新更新

  • 我很确定我已经找到了一种方法,一旦它们停止相互通信,就可以将设备重新同步.我将在下面用所有细节更新我的答案.我完全希望你们都觉得这很有帮助.花了将近2个月的试验和错误来解决这个问题.所以请参考并与其他有类似问题的人分享,让设备再次通过iCloud相互交谈.我花了很长时间才弄清楚这一点,所以我非常乐意尽可能多地保存其他开发人员来创建自己的make-shift修复程序.

另一个有助于正确设置的补充

  • 我发现在更新具有与帐户关联的iCloud数据的应用程序后,打开它时可能会导致崩溃,因为iCloud数据将尝试立即合并到设备中(设备尚未设置其持久存储).现在我已经加入 @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)

iphone core-data ios5 icloud

38
推荐指数
2
解决办法
1万
查看次数

CoreData:错误:无法在NSManagedObject类上调用指定的初始化程序

我对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 objective-c initializer

38
推荐指数
3
解决办法
4万
查看次数