Core data and distributed object
Dear Community. I try to release multiuser using core data. I was using a client-server bonjour connectivity. How it work now : server part:
- (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 a开发者_如何学Golloc] 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;
}
Client part:
- (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];
}
Entity Carrier is a head of my tree objects. Database size is 220M. Everything work but i can't using that way because: 1. while i have update on server, i make delete for some "Carrier" objects, and this case product
CoreData could not fulfill a fault for
error if i try to sort my array on client side by column.
This way product a lot of freezes of server and client user interface while data loading. I understand, what i can put a progress loading, but i don't understand how i can know a data progress sending, this is working behind scenes as i see. Network connection is perfect, and on local computer running client and server i have same effects.
I tried too many times to make sync my managedobjectcontext for client computer (probably this may be a better way), but i have some point, which i don't understand there: a) how i can do a whole copy from external moc to local moc b) may i create on server other copy of local moc to using by remote clients? attemt to create moc and attach to existing persistent store was fail, moc was empty...
Maybe somebody can share experience or suggest a good examples?
精彩评论