第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

核心數(shù)據(jù):刪除實體所有實例的最快方法

核心數(shù)據(jù):刪除實體所有實例的最快方法

揚帆大魚 2019-08-14 16:48:28
核心數(shù)據(jù):刪除實體所有實例的最快方法我正在使用Core Data在本地持久保存Web服務(wù)調(diào)用的結(jié)果。Web服務(wù)返回完整的對象模型,比方說,“汽車” - 可能是大約2000個(我不能讓W(xué)eb服務(wù)返回任何小于1或所有汽車。下次打開我的應(yīng)用程序時,我想通過再次調(diào)用所有汽車的Web服務(wù)來刷新Core Data持久化副本,但是為了防止重復(fù),我需要首先清除本地緩存中的所有數(shù)據(jù)。是否有更快的方法來清除托管對象上下文中特定實體的所有實例(例如“CAR”類型的所有實體),或者我是否需要查詢它們,然后遍歷結(jié)果以刪除每個實例,然后保存?理想情況下,我可以說刪除所有實體是Blah的地方。
查看完整描述

3 回答

?
Smart貓小萌

TA貢獻1911條經(jīng)驗 獲得超7個贊

iOS 9及更高版本:

iOS 9添加了一個名為的新類NSBatchDeleteRequest,它允許您輕松刪除與謂詞匹配的對象,而無需將它們?nèi)考虞d到內(nèi)存中。這是你如何使用它:


斯威夫特2

let fetchRequest = NSFetchRequest(entityName: "Car")

let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)


do {

    try myPersistentStoreCoordinator.executeRequest(deleteRequest, withContext: myContext)

} catch let error as NSError {

    // TODO: handle the error

}

Objective-C的

NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Car"];

NSBatchDeleteRequest *delete = [[NSBatchDeleteRequest alloc] initWithFetchRequest:request];


NSError *deleteError = nil;

[myPersistentStoreCoordinator executeRequest:delete withContext:myContext error:&deleteError];

有關(guān)批量刪除的更多信息可以在WWDC 2015的“核心數(shù)據(jù)中的新內(nèi)容”會話中找到(從~14:10開始)。


iOS 8及更早版本:

獲取所有并刪除所有:


NSFetchRequest *allCars = [[NSFetchRequest alloc] init];

[allCars setEntity:[NSEntityDescription entityForName:@"Car" inManagedObjectContext:myContext]];

[allCars setIncludesPropertyValues:NO]; //only fetch the managedObjectID


NSError *error = nil;

NSArray *cars = [myContext executeFetchRequest:allCars error:&error];

[allCars release];

//error handling goes here

for (NSManagedObject *car in cars) {

  [myContext deleteObject:car];

}

NSError *saveError = nil;

[myContext save:&saveError];

//more error handling here


查看完整回答
反對 回復(fù) 2019-08-14
?
慕絲7291255

TA貢獻1859條經(jīng)驗 獲得超6個贊

更清潔和通用:添加此方法:

- (void)deleteAllEntities:(NSString *)nameEntity{
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:nameEntity];
    [fetchRequest setIncludesPropertyValues:NO]; //only fetch the managedObjectID

    NSError *error;
    NSArray *fetchedObjects = [theContext executeFetchRequest:fetchRequest error:&error];
    for (NSManagedObject *object in fetchedObjects)
    {
        [theContext deleteObject:object];
    }

    error = nil;
    [theContext save:&error];}


查看完整回答
反對 回復(fù) 2019-08-14
?
Qyouu

TA貢獻1786條經(jīng)驗 獲得超11個贊

Swift 3中重置實體:

func resetAllRecords(in entity : String) // entity = Your_Entity_Name
    {

        let context = ( UIApplication.shared.delegate as! AppDelegate ).persistentContainer.viewContext
        let deleteFetch = NSFetchRequest<NSFetchRequestResult>(entityName: entity)
        let deleteRequest = NSBatchDeleteRequest(fetchRequest: deleteFetch)
        do
        {
            try context.execute(deleteRequest)
            try context.save()
        }
        catch
        {
            print ("There was an error")
        }
    }


查看完整回答
反對 回復(fù) 2019-08-14
  • 3 回答
  • 0 關(guān)注
  • 590 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號