亲爱的社区。 我尝试使用核心数据发布多用户。我正在使用客户端 - 服务器bonjour连接。它现在如何运作: 服务器部分:
- (void)startBroadcasting;
{
receiveSocket = [[NSSocketPort alloc] init];
int receivePort = [self portFromSocket:receiveSocket];
NSLog(@"NETWORK: port is %d",receivePort);
myConnection = [[NSConnection alloc] initWithReceivePort:receiveSocket
sendPort:nil];
[myConnection setRootObject:self];
myService = [[NSNetService alloc] initWithDomain:kDomainName
type:kServiceName
name:kServerName
port:receivePort];
[myService setDelegate:self];
[myService publish];
}
- (byref NSArray*)allObjects
{
NSManagedObjectContext *context = [self managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Carrier"
inManagedObjectContext:context];
[request setEntity:entity];
[request setIncludesSubentities:YES];
NSError *error = nil;
NSArray *objects = [context executeFetchRequest:request error:&error];
[request release], request = nil;
if (error) {
NSLog(@"%@:%@ error: %@", [self class], NSStringFromSelector(_cmd), error);
return nil;
}
return objects;
}
客户端部分:
- (void)netServiceDidResolveAddress:(NSNetService*)service
{
NSLog(@"%@:%@ netServiceDidResolveAddress find something\n", [self class], NSStringFromSelector(_cmd));
NSConnection *clientConnection = nil;
NSSocketPort *socket = nil;
//NSData *address = [[service addresses] lastObject];
//u_char family = ((struct sockaddr*)[address bytes])->sa_family;
//socket = [[NSSocketPort alloc] initRemoteWithProtocolFamily:family
// socketType:SOCK_STREAM
// protocol:IPPROTO_TCP
// address:address];
socket = [[NSSocketPort alloc] initRemoteWithTCPPort:[service port]
host:[service hostName]];
clientConnection = [NSConnection connectionWithReceivePort:nil
sendPort:socket];
[clientConnection enableMultipleThreads];
[self setServer:[clientConnection rootProxy]];
[socket release], socket = nil;
[service stop];
[service release];
// testing connection:
[server ping];
NSArray *allObjects = [server allObjects];
[carriersArrayVisible setContent:allObjects];
}
实体承运人是我的树对象的头。 数据库大小为220M。 一切正常但我无法使用这种方式,因为: 1.当我在服务器上更新时,我删除了一些“Carrier”对象,以及本案例产品
CoreData无法解决
的错误
错误,如果我尝试按列在客户端排序我的数组。
这种方式产生了数据加载时服务器和客户端用户界面的大量冻结。我理解,我可以把进度加载,但我不明白我怎么知道数据进度发送,这是在我看到的幕后工作。网络连接是完美的,在运行客户端和服务器的本地计算机上我有相同的效果。
我尝试了太多次来为客户端计算机同步我的managedobjectcontext(可能这可能是一种更好的方式),但我有一点,我不明白: a)我如何从外部moc到本地moc完成整个副本 b)我可以在服务器上创建本地moc的其他副本以供远程客户端使用吗? attemt创建moc并附加到现有持久存储失败,moc为空......
也许有人可以分享经验或提出一个很好的例子?