2013-11-16 4 views
0

회사라는 엔터티를 사용하여 핵심 데이터 배열을 정렬하려고합니다. 그러나, 내 애플 리케이션은 현재 내가 사용하는 코드로 데이터를 정렬하지 않습니다. 무슨 일이야?핵심 데이터 정렬 오류

여기
2013-11-15 17:07:59.615 Minutes[70925:a0b] (
    "<NSManagedObject: 0x89d4d40> (entity: Device; id: 0x89bdff0 <x-coredata://29EA13EB-909E-46E8-B8FD-C71A9B6436CE/Device/p2> ; data: {\n company = Reading;\n name = 1;\n version = \"Minutes of Harry Potter\";\n})", 
    "<NSManagedObject: 0x89d4ad0> (entity: Device; id: 0x8986ff0 <x-coredata://29EA13EB-909E-46E8-B8FD-C71A9B6436CE/Device/p1> ; data: {\n company = Service;\n name = 2;\n version = \"Minutes of Community\";\n})", 
    "<NSManagedObject: 0x89d4db0> (entity: Device; id: 0x8997950 <x-coredata://29EA13EB-909E-46E8-B8FD-C71A9B6436CE/Device/p3> ; data: {\n company = Service;\n name = 0;\n version = \"Minutes of Blank\";\n})" 
) 

배열을 정렬하는 데 사용하는 코드이다 : 여기서

는 오류가 미리

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 

    // Fetch the devices from persistent data store 
    NSManagedObjectContext *managedObjectContext = [self managedObjectContext]; 
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Device"]; 
    self.devices = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy]; 

    fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey: 
             @"company" ascending:YES]]; 

    [self.tableView reloadData]; 
} 

감사!

편집 : 예상대로

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    return self.devices.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    // Configure the cell... 
    NSManagedObject *device = [self.devices objectAtIndex:indexPath.row]; 
    [cell.textLabel setText:[NSString stringWithFormat:@"%@ %@", [device valueForKey:@"name"], [device valueForKey:@"version"]]]; 
    [cell.detailTextLabel setText:[device valueForKey:@"company"]]; 

    return cell; 
} 
+0

배열이 "회사"속성과 관련하여 올바르게 정렬 된 것으로 보입니다. –

+1

나중에 정렬 대신 가져 오기 요청에 정렬 설명자를 직접 설정할 수 있습니다. –

답변

1

작품 : 여기가 데이터를 표시하는 방법을 보여주는 몇 가지 코드입니다. "독해"< = "서비스"< = "서비스".

self.devicessortedPeople으로 정렬하지만 테이블 뷰의 데이터 소스에 정렬되지 않은 self.devices이 표시됩니다.

정렬 요청자를 가져 오기 요청에 연결하고 sortedPeople 배열을 제거하면 코드가 다시 정상적으로 작동합니다.

fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey: 
     @"company" ascending:YES]]; 

또한 셀 메소드에 유형이없는 것은 아니며 NSManagedObjectModel이 아닌 NSManagedObject 여야합니다. Device이라는 하위 클래스를 사용하는 것이 좋습니다 (Xcode에서 자동으로 생성 할 수 있음). Key-Value-Coding 대신 속성을 사용합니다.

편집 :

BTW. 후에 변경하면 사용했던 내용에 변경 내용이 포함되지 않습니다. 예 :

x = 1; 
y = x + 1; // y == 2 
x = 3;  // y == 2 (nothing changed for y) 
y = x + 1; // y == 4 (y was used after the change) 

페치 (y) 및 해당 페치 요청 (x)을 실행하여 생성 된 페치 결과도 마찬가지입니다.

+0

예. 그렇게 보일 것입니다. 그러나 응용 프로그램을 실행하면 해당 방법이 표시되지 않습니다. 최근에 가장 최근에 표시됩니다. 생각? – PanicDev

+0

표시 방법을 보여줍니다. – Mundi

+0

정보로 질문을 업데이트했습니다. – PanicDev