TITLE
    Creating a WebObjects Application With a Single Editing Context
Article ID:
Created:
Modified:
72646
9/12/98
12/1/98

TOPIC

    This article gives an example of an application written in WebScript that is configured to share a single EditingContext across multiple sessions, each with its own WODisplayGroup. It assumes that you are using WebObjects 3.5.1.


DISCUSSION

    By default, WebObjects applications use one EditingContext per session. All of an application's EditingContexts share the same ObjectStoreCoordinator. This configuration means that multiple users can edit the database without affecting the other users' sessions. For most applications, this is the most desirable configuration.

    However, for read-only applications where a user's session will never affect the database, sharing a single editing context across multiple sessions will improve performance. Please note that, in this example, you do not need to drag an entity from EOModeler to a Component in WebObjects builder. Your project should find your eomodel if you include it in the "Resources" suitcase in ProjectBuilder. If you want to explicitly create EOEditingContext's supporting objects, example code to do so is given below:

    //========= Application.wos =================================

    EOEditingContext *myEditingContext;

    - init
    {
    [super init];
    myEditingContext = [[EOEditingContext alloc] init];
    return self;
    }


    //========= Main.wos ======================================

    EODatabaseDataSource *myDataSource;
    WODisplayGroup *myDisplayGroup;

    - (id)init
    {
    myDataSource = [[EODatabaseDataSource alloc] initWithEditingContext:[[self application] myEditingContext] entityName:@"MyEntity"];
    myDisplayGroup = [[WODisplayGroup alloc] init];
    [myDisplayGroup setDataSource:myDataSource];
    }
    //========================================================



    //================= Application.wos ==============================
    // EOF Objects to be created
    // For clarity, make them instance variables
    EOEditingContext *myEditingContext;
    EOObjectStoreCoordinator *objectStoreCoordinator;
    EODatabaseContext *dataBaseContext;
    EODatabase *database;
    EOModel *dataModel;


    - init
    {
    [super init];

    /*Allocating and initializing the model from a file. */
    dataModel = [[EOModel alloc] initWithContentsOfFile: @"/NextLibrary/WebServer/htdocs/WebObjects/MyProject/MyModel.eomodeld"];

    /* Setting up the EO Database */
    database = [[EODatabase alloc] initWithModel:dataModel];

    /* setting up the database context with the model and editing context */
    dataBaseContext = [[EODatabaseContext alloc] initWithDatabase: database];

    objectStoreCoordinator = [EOObjectStoreCoordinator defaultCoordinator];
    [objectStoreCoordinator addCooperatingObjectStore:dataBaseContext];

    /*setting up the Editing Context with its parent object store (the DataBaseContext) */
    myEditingContext = [[EOEditingContext alloc] initWithParentObjectStore:objectStoreCoordinator];

    return self;
    }

Document Information
Product Area: WebObjects
Category: WebObjects 3.5.1
Sub Category: Examples

Copyright © 2000 Apple Computer, Inc. All rights reserved.