上下文:
我需要比较它们。
问题:
目标:
很明显,我想用新内容更新我的本地数据库。它是一个SQLite数据库。 我只想更新已更改的字段。说友谊#147有更新的分数,我只是更新相应的对象147,我很好。但知道发生了什么变化阻止了我。
目前情况:
现在,我只是始终删除它并使用新内容重新创建它,正如您现在可能尖叫的那样,非常不理想。这是我的代码(带注释)
+ (void)compareFriendshipsFromDb:(NSMutableArray*)friendshipsFromDb andInsert:(NSMutableArray*)friendshipsOnline{
BOOL update = NO; //It tells us if there is new stuff
NSUInteger x = 0; //It helps us compare later.
if ([friendshipsFromDb count] != [friendshipsOnline count]){
update = YES; //The count is different, there MUST be something different, we update.
}
else{
x = [friendshipsOnline count]; //same count, but not specifically identical.
// i'm saving that count in 'x' because its the up-to-date count.
}
if (x > 0 && update == NO){ //This means that the count was identical and that there were friendships to update
for(int i = 0; i < x ; i ++){
//TODO: Compare with PREDICATES ? I read online that it's very powerful/optimal
//This is how I compare right now. It's always updating right now...
if (![friendshipsFromDb containsObject:[friendshipsOnline objectAtIndex:i]]){
update = YES;
break;
}
}
}
// Count was at zero (no friendships online) and none were found in DB. Pretty much alone.
if (x == 0 && update == NO){
DLog(@"There are no friends to compare");
}
//This is my update process, doesn't really matter.
if (update){
[DatabaseManager dropAndRecreateTable:@"friendships"];
[DatabaseManager insertFriendships:friendshipsOnline];
}
//And this calls the end of this part of the update, doesn't really matter either.
//I left these just so you have the complete process.
else {
[[NSNotificationCenter defaultCenter]postNotificationName:NOTIF_FRIENDSHIPS_DONE object:self];
[[NSNotificationCenter defaultCenter]postNotificationName:NOTIF_HOME_RELOAD object:self];
}
}
此外,我觉得将每个对象与另一个对象进行比较(两个for循环)只是一个完全矫枉过正的考虑因素而浪费资源: - 我有多个要比较的属性 - 大多数比较都没用,因为我不需要将友谊X与友谊Y进行比较。我需要将它与友谊X的新版本进行比较。
你去了,这就是我现在所拥有的一切,但请务必在评论中提问。对于有经验的程序员来说,这看起来相当容易,但我不知道如何在这里取得成功。我的残酷方法有效,但只是充满了不必要的过程。
我都是耳朵!
编辑:相关问题:
我总是会在该特定表格中使用大约10到50个条目。 是保持它的方式更有效率,或者将ifs中的内部forloops与所有地方进行比较,并进行唯一的db调用(但可能连续15次)? 连续15次打开更新 - 关闭db或者只是粗暴地删除并且每次都以数组形式插入所有内容(但只有一次)会更加繁重吗?
答案 0 :(得分:0)
考虑一下你想要比较产品对象的数组
Prodcut.h
@interface Prodcut : NSObject
@property NSString *name;
@property NSString *firstName;
-(BOOL)isEqual:(Prodcut *)object;
@end
Product.m
#import "Prodcut.h"
@implementation Prodcut
-(BOOL)isEqual:(Prodcut *)object{
int i = 0;
if ([self.name isEqual:object.name]) {
i++;
if ([self.firstName isEqualToString:object.firstName]) {
i++;
}
}
if (i == 2) {
return YES;
}
else return NO;
}
@end
压缩逻辑
Prodcut *prod1 = [[Prodcut alloc] init];
prod1.name = @"name";
prod1.firstName = @"fn1";
Prodcut *prod2 = [[Prodcut alloc] init];
prod2.name = @"name";
prod2.firstName = @"fn2";
Prodcut *prod3 = [[Prodcut alloc] init];
prod3.name = @"name";
prod3.firstName = @"fn1";
Prodcut *prod4 = [[Prodcut alloc] init];
prod4.name = @"name";
prod4.firstName = @"fn2";
NSArray *array1 = [NSArray arrayWithObjects:prod1,prod2,nil];
NSArray *array2 = [NSArray arrayWithObjects:prod3,prod4,nil];
int i=0;
if (array1.count == array2.count) {
for (Prodcut *pro in array1 ) {
for (Prodcut *innerPro in array2) {
if ([pro isEqual:innerPro]) {
i++;
break;
}
}
}
if (i == array1.count) {
NSLog(@"same");
}
else{
NSLog(@"Not Same");
}
}
else{
NSLog(@"Not same");
}