forked from nickynick/ArrayDiff
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathItem.m
More file actions
128 lines (99 loc) · 4.17 KB
/
Item.m
File metadata and controls
128 lines (99 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//
// Item.m
// ArrayDiffExample
//
// Created by Nick Tymchenko on 13/04/14.
// Copyright (c) 2014 Nick Tymchenko. All rights reserved.
//
#import "Item.h"
#import "AppDelegate.h"
@implementation Item
@dynamic name;
- (NSString *)sectionTitle {
return [[self.name substringToIndex:1] uppercaseString];
}
+ (NSManagedObjectContext *)managedObjectContext {
return ((AppDelegate *)[UIApplication sharedApplication].delegate).managedObjectContext;
}
+ (void)setupItemsWithNames:(NSArray *)names {
NSManagedObjectContext *context = [self managedObjectContext];
NSArray *existingItems = [context executeFetchRequest:[Item requestForSortedItems] error:NULL];
for (Item *item in existingItems) {
[context deleteObject:item];
}
for (NSString *name in names) {
Item *item = [[Item alloc] initWithEntity:[NSEntityDescription entityForName:@"Item" inManagedObjectContext:context] insertIntoManagedObjectContext:context];
item.name = name;
}
[context save:NULL];
}
+ (void)addRandomItem {
NSManagedObjectContext *context = [self managedObjectContext];
Item *item = [[Item alloc] initWithEntity:[NSEntityDescription entityForName:@"Item" inManagedObjectContext:context] insertIntoManagedObjectContext:context];
item.name = [self randomName];
[context save:NULL];
}
+ (void)updateRandomItems {
NSManagedObjectContext *context = [self managedObjectContext];
NSArray *existingItems = [context executeFetchRequest:[Item requestForSortedItems] error:NULL];
if ([existingItems count] == 0) {
return;
}
NSUInteger count = 1 + arc4random_uniform(4);
for (NSUInteger i = 0; i < count; ++i) {
NSUInteger randomIndex = arc4random_uniform((int32_t)[existingItems count]);
Item *item = existingItems[randomIndex];
item.name = [self randomName];
}
[context save:NULL];
}
+ (void)deleteRandomItem {
NSManagedObjectContext *context = [self managedObjectContext];
NSArray *existingItems = [context executeFetchRequest:[Item requestForSortedItems] error:NULL];
if ([existingItems count] == 0) {
return;
}
NSUInteger randomIndex = arc4random_uniform((int32_t)[existingItems count]);
Item *item = existingItems[randomIndex];
[context deleteObject:item];
[context save:NULL];
}
+ (Item *)existingItemWithName:(NSString *)name {
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Item"];
request.predicate = [NSPredicate predicateWithFormat:@"%K == %@", @"name", name];
NSArray *result = [[self managedObjectContext] executeFetchRequest:request error:NULL];
return [result firstObject];
}
+ (NSFetchRequest *)requestForSortedItems {
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Item"];
request.sortDescriptors = @[ [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)] ];
return request;
}
+ (NSArray *)fetchSortedItemSections {
NSManagedObjectContext *context = [self managedObjectContext];
NSArray *sortedItems = [context executeFetchRequest:[Item requestForSortedItems] error:NULL];
NSMutableDictionary *sectionsByKeys = [NSMutableDictionary dictionary];
NSMutableOrderedSet *sectionKeys = [NSMutableOrderedSet orderedSet];
for (Item *item in sortedItems) {
[sectionKeys addObject:item.sectionTitle];
NNMutableSection *section = sectionsByKeys[item.sectionTitle];
if (!section) {
section = [[NNMutableSection alloc] initWithKey:item.sectionTitle objects:nil];
sectionsByKeys[item.sectionTitle] = section;
}
[section.objects addObject:item];
}
NSMutableArray *sections = [NSMutableArray array];
for (NSString *sectionKey in sectionKeys) {
[sections addObject:sectionsByKeys[sectionKey]];
}
return sections;
}
#pragma mark - Private
+ (NSString *)randomName {
char letter = 'A' + arc4random_uniform(5);
char digit1 = '0' + arc4random_uniform(10);
char digit2 = '0' + arc4random_uniform(10);
return [NSString stringWithFormat:@"%c%c%c", letter, digit1, digit2];
}
@end