0

I'm trying to implement persistence for a simple data model of an iOS app. It's kind of a database, but with a small amount of data (200-500 entries). I think core data is no option here.

The data model looks somehow like this (currently I use NSDictionary with NSArray):

// Note: NSStrings and NSNumbers are simplified for readability (no @)
@{ "Companys" : @[
          @{ "ID"           : 1,
             "Company name" : "ABC Inc.",
             "Website"      : "http://www.abc.com"
           },
          @{ "ID"           : 2,
             "Company name" : "XYZ Inc.",
             "Website"      : "http://www.xyz.com"
           },
          @{ "ID"           : 3,
             "Company name" : "123 Inc.",
             "Website"      : "http://www.123.net"
           },
          // ... about 90 companies
          ],
    "Employees": @[
          @{ "ID"           : 1,
             "First name"   : "John",
             "Last name"    : "Doe"
           },
          @{ "ID"           : 2,
             "First name"   : "David",
             "Last name"    : "Green"
           },
          // ... about 500 employees
    ]
}

Now I'm facing the problem of relationships (which employees work in which company). I thought of adding and NSArray to the company NSDictionary which holds the IDs of the employees of that company. But somehow I'm afraid of bad performance as I would need to enumerate all companies each time I want to get the company which an employee works for.

Is there any better solutions for saving this kind of data? I think using SQLite or Core Data would be way to over-engineered and would cost too much development time...

EDIT: Please don't suggest Core Data in your comments and answers. Core Data is not an option as the data is very static with only few changes, and sorting is not needed. Also iCloud synchronization might be an option. Core Data has plenty of bugs when using iCloud...

4

2 に答える 2

3

Core Data はクールです。恐れないでください :) Core Data を使用することをお勧めします。

しかし、Apple によって広められた最新のテクノロジーを学びたくない場合は、データを plist ファイルに保存する必要があります。多くのチュートリアルがあります。ここに素敵なサンプルがあります。

于 2013-07-26T14:40:48.400 に答える
2

データベースが Core Data に対して小さすぎる場合は、うまくいくかもしれませNSCodingん。(実行時に) 辞書や配列にデータを格納することはお勧めしません。代わりに (データ モデルがそれほど複雑ではないため)、Employee や Company などのモデル オブジェクトを実装します。これらのモデル オブジェクトは、NSCodingプロトコルを実装する必要があります。次のようになります。

@interface Employee : NSObject <NSCoding>
@property (nonatomic) NSString *firstName;
@property (nonatomic) NSString *lastName;
@property (nonatomic, weak) Company *company;
@end

@implementation Employee
- (id)initWithCoder:(NSCoder *)coder {
    self = [super init];
    if (self) {
        self.firstName = [coder decodeObjectForKey:@"FirstName"];
        self.lastName = [coder decodeObjectForKey:@"LastName"];
        self.company = [coder decodeObjectForKey:@"Company"];
    }
}
- (void)encodeWithCoder:(NSCoder *)coder {
    [coder encodeObject:self.firstName forKey:@"FirstName"];
    [coder encodeObject:self.lastName forKey:@"LastName"];
    [coder encodeConditionalObject:self.company forKey:@"Company"];
}
@end

@interface Company : NSObject <NSCoding>
@property (nonatomic) NSString *name;
@property (nonatomic) NSString *website;
@property (nonatomic) NSArray *employees;
@end

@implementation Company
- (id)initWithCoder:(NSCoder *)coder {
    self = [super init];
    if (self) {
        self.name = [coder decodeObjectForKey:@"Name"];
        self.website = [coder decodeObjectForKey:@"Website"];
        self.employees = [coder decodeObjectForKey:@"Employees"];
    }
}
- (void)encodeWithCoder:(NSCoder *)coder {
    [coder encodeObject:self.name forKey:@"Name"];
    [coder encodeObject:self.website forKey:@"Website"];
    [coder encodeObject:self.employees forKey:@"Employees"];
}
@end

これで、次のようにすべてを簡単に保存できますNSKeyedArchiver

[NSKeyedArchiver archiveRootObject:companiesArray toFile:somePath];

そして、それらをロードしますNSKeyedUnarchiver

NSArray *companiesArray = [NSKeyedUnarchiver unarchiveObjectWithFile:somePath];
于 2013-07-26T14:42:48.810 に答える