2014-07-08 3 views
1

코어 데이터에 저장된 브랜드 목록이 있습니다. 각 브랜드는 여러 내용과 연관되어 있습니다. 콘텐츠에는 콘텐츠가 다운로드되었는지 여부를 나타내는 데 사용되는 downStatus이라는 플래그가 있습니다. 다음 방법은여러 값으로 코어 데이터 정렬

-(void)getDownloadedBrands{ 

AppDelegate *aAppDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; 

NSMutableArray *aBrands = [[NSMutableArray alloc] init]; 


NSEntityDescription *entity = 
[NSEntityDescription entityForName:@"Brand" inManagedObjectContext:aAppDelegate.managedobjectcontext]; 
NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
[request setEntity:entity]; 

NSSortDescriptor *sort=[[NSSortDescriptor alloc]initWithKey:@"brandName" ascending:YES]; 
//NSSortDescriptor *sort1=[[NSSortDescriptor alloc]initWithKey:@"downStatus" ascending:YES]; 

[request setSortDescriptors:[NSArray arrayWithObjects:sort, nil]]; 

aBrands =(NSMutableArray *)[aAppDelegate.managedobjectcontext executeFetchRequest:request error:nil]; 
[request release]; 
[sort release]; 
NSLog(@"%@",aBrands); 

brands = [[NSMutableArray alloc]initWithArray:aBrands]; 

aAppDelegate.dbBrandArr = brands; 

[self loadGridView]; 

} 

가 지금은 내용에있는 downStatus를 사용하여 정렬 할 브랜드로 분류 핵심 데이터의 모든 브랜드를 가져 오는 데 사용됩니다. 따라서 알파벳순으로 다운로드 한 브랜드와 알파벳순으로 다운로드하지 않은 브랜드가됩니다. downStatus은 다운로드하지 않은 0에 대해 1의 값을 2 개 받아들입니다. 도와주세요. 그것을 할 수

답변

1

한 가지 방법은 NSPredicate1/0 on downStatus 속성을 사용하여 CoreData에 2 fetchRequests을하고 같은 알파벳 sortDescriptor을 사용하는 것입니다.

이제 1 배열에 0 배열을 추가하여 새 배열을 만듭니다. 코드에서는이 같은 보일 것이다 :

-(void)getDownloadedBrands{ 
    AppDelegate *aAppDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; 

    //2 arrays 
    NSMutableArray *aBrands0 = [[NSMutableArray alloc] init]; 
    NSMutableArray *aBrands1 = [[NSMutableArray alloc] init]; 
    //set entity en make requests 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Brand" inManagedObjectContext:aAppDelegate.managedobjectcontext]; 
    NSFetchRequest *request0 = [[NSFetchRequest alloc] init]; 
    NSFetchRequest *request1 = [[NSFetchRequest alloc] init]; 
    [request0 setEntity:entity]; 
    [request1 setEntity:entity]; 
    //create sortDescriptor alphabetically 
    NSSortDescriptor *sort=[[NSSortDescriptor alloc]initWithKey:@"brandName" ascending:YES]; 
    //create predicates on downStatus property 
    NSPredicate *predicate0 = [NSPredicate predicateWithFormat:@"downStatus == %@", [NSNumber numberWithBool:0]]; 
    NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"downStatus == %@", [NSNumber numberWithBool:1]]; 
    //set predicates to the requests 
    [request0 setPredicate:predicate0]; 
    [request1 setPredicate:predicate1]; 
    //set sortDescriptor to both requests 
    [request0 setSortDescriptors:[NSArray arrayWithObjects:sort, nil]]; 
    [request1 setSortDescriptors:[NSArray arrayWithObjects:sort, nil]]; 
    //fetch arrays with downStatus 1/0 
    aBrands0 =(NSMutableArray *)[aAppDelegate.managedobjectcontext executeFetchRequest:request0 error:nil]; 
    aBrands1 =(NSMutableArray *)[aAppDelegate.managedobjectcontext executeFetchRequest:request1 error:nil]; 
    //release requests // NOT USING ARC?? 
    [request0 release]; 
    [request1 release]; 
    [sort release]; 
    //log results 
    NSLog(@"aBrands0: %@",aBrands0); 
    NSLog(@"aBrands1: %@",aBrands1); 
    //add object 0 array to 1 array 
    NSArray *combinedArray = [aBrands1 arrayByAddingObjectsFromArray:aBrands0]; 
    //copy array to brands 
    brands = [[NSMutableArray alloc]initWithArray:combinedArray]; 
    //set property on appDelegate 
    aAppDelegate.dbBrandArr = brands; 
    //reload tableView 
    [self loadGridView]; 
} 

- 당신이있는 UITableViewController 결과 페치를 사용하려는 경우

-(void)getDownloadedBrands{ 
    AppDelegate *aAppDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; 

    NSMutableArray *aBrands = [[NSMutableArray alloc] init]; 


    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Brand" inManagedObjectContext:aAppDelegate.managedobjectcontext]; 
    NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
    [request setEntity:entity]; 

    NSSortDescriptor *sort=[[NSSortDescriptor alloc]initWithKey:@"brandName" ascending:YES]; 
    //NSSortDescriptor *sort1=[[NSSortDescriptor alloc]initWithKey:@"downStatus" ascending:YES]; 

    [request setSortDescriptors:[NSArray arrayWithObjects:sort, nil]]; 

    aBrands =(NSMutableArray *)[aAppDelegate.managedobjectcontext executeFetchRequest:request error:nil]; 

    NSMutableArray *aBrands0 = [[NSMutableArray alloc] init]; 
    NSMutableArray *aBrands1 = [[NSMutableArray alloc] init]; 
    for (NSDictionary *dict in aBrands) { 
     if ([dict valueForKeyPath:@"downStatus"] == YES) 
     { 
      //add to 1 array 
      [aBrands1 addObject:dict]; 
     } 
     else 
     { 
      //add to 0 array 
      [aBrands0 addObject:dict]; 
     } 
    } 

    //sort arrays 
    NSArray * array1 = [aBrands1 sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]]; 
    NSArray * array0 = [aBrands0 sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]]; 
    NSArray * allArray = [aBrands1 arrayByAddingObjectsFromArray:aBrands0]; 

    //combine arrays 

    //copy array to brands 
    brands = [[NSMutableArray alloc]initWithArray:combinedArray]; 
    //set property on appDelegate 
    aAppDelegate.dbBrandArr = brands; 
    //reload tableView 
    [self loadGridView]; 

    [request release]; 
    [sort release]; 
    NSLog(@"%@",aBrands); 
} 
+0

안녕하세요, 답장을 보내 주셔서 감사합니다. 이제는이 오류를 알려줍니다. *** 캐치되지 않은 예외로 인해 앱이 종료 중입니다. 'NSInvalidArgumentException', 이유 : 'keypath downStatus가 엔티티에 없습니다'라는 메시지가 표시됩니다. 'downStatus'가 Content에 있기 때문에 브랜드 없음 – LeXeR

+0

Core Data 객체에 downStatus 속성이 없습니까? 핵심 데이터에서 어떤 종류의 데이터를 가져오고 있습니까? 나는 사전을 추측하고있다. 그렇다면 다른 접근법이 필요할 것이다. – Michael

+0

실제로'Content'는'Brand'와 연관되어 있고 각 내용은'downStatus' 속성이'1' 또는'0'으로되어 있습니다. downStatus가 1 또는 0이고 brandName으로 정렬 된 모든 브랜드를 가져와야합니다. 나는 여기에 어떤 관계를 언급해야 할 수도 있지만 그것을 어떻게 해야할지 모르겠다. – LeXeR

0

키 downStatus과 편집, 추가 대답있는 NSDictionary를 들어, 당신은 사용할 수 있습니다 downStatus에 대한 fetchedResultsController의 sectionNameKeyPath를 지정하고 브랜드 이름에 대한 정렬 설명자를 유지하십시오. 당신의 NSManagedObject의 헤더 파일이

NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] 
initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext 
sectionNameKeyPath:@"customSectionTitle" cacheName:nil]; 

장소 : 같이 할 수

-(NSString*)customSectionTitle; 

그리고이하는 .m에서 :

-(NSString*)customSectionTitle{ 
    if ([downStatus boolValue] == YES) { 
     return @"Downloaded"; 
    } 
    else {return @"Not Downloaded";} 
} 

당신이 사용하지 않을 경우 fetchedResultsController, 먼저 downStatus로 정렬 한 다음 브랜드 이름으로 정렬합니다.

+0

아니요, 어떤 tableLayout도 사용하지 않고 있습니다.이 데이터로 GridLayout을 만듭니다. – LeXeR

+0

나는 이것이 나의 대답의 마지막 문장으로 덮여 있다고 믿는다. –

+0

오 .... 그걸 보지 못했습니다. 네, 그렇게해야 할 수도 있습니다. – LeXeR